codeGK
Code it ALL
jQuery Ancestors - CodeGK
♦ jQuery Ancestors

♣ About jQuery Ancestors

Ancestor as the name suggests, it refers to tag that is hierarchically above the current tag.
There exist various ancestor methods in jQuery.
Below is a tabular explanation of ancestor methods in jQuery
Table Info
MethodFunctioning
parent()Selects a tag just above in hierarchy
parents()Selects all the tags above in hierarchy
parentsUntil()Selects all the tags above in hierarchy untill the specified tag

♣ Example1: jQuery parent()

Below is the representation of jQuery parent() Syntax.
Basic Syntax
$(selector).parent().event(function(){
//code_block
});
Syntax Explanation:
selector : the element of which parent is to be selected
event : this can be events(click, hover, etc) or css or addClass, etc
code_block : code to executed on parent tag
In this example, the just parent border of level 4 is blue-colored.
Example
Input:
Output:

♣ Example2: jQuery parents()

Below is the representation of jQuery parents() Syntax.
Basic Syntax
$(selector).parents().event(function(){
//code_block
});
Syntax Explanation:
selector : the element of which parents are to be selected
event : this can be events(click, hover, etc) or css or addClass, etc
code_block : code to executed on all parent tags
In this example, all the parent borders of level 4 are blue-colored, including the body and html tags.
Example
Input:
Output:

♣ Example3: jQuery parentsUntil()

Below is the representation of jQuery parentsUntil() Syntax.
Basic Syntax
$(selector).parentsUntil(limitTag).event(function(){
//code_block
});
Syntax Explanation:
selector : the element of which parents are to be selected
limitTag : tag upto which parents are supposed to be selected
event : this can be events(click, hover, etc) or css or addClass, etc
code_block : code to executed on all the parent tags
In this example, all the parent borders of level 4 are blue-colored until level 2.
Example
Input:
Output:
Prev_Lesson