The parents Method
The parents method gets all the parents of an element - not only the immediate one, but also the ancestor, great-grandparent, and so on up to the root element (that is, up to the html tag).
Syntax
Getting all parents for elements:
.parents();
You can also select parents only if they satisfy a given selector:
.parents(selector);
Example
Let's find the element #test, then find all its parents with parents and if they are parents of div with class www, then prepend them with the text '!' with prepend:
<div>
<div class="www">
<div class="www">
<p id="test">text</p>
</div>
</div>
</div>
$('#test').parents('div.www').prepend('!');
HTML the code will look like this:
<div>
<div class="www">
!
<div class="www">
!<p id="test">text</p>
</div>
</div>
</div>
See also
-
method
parent,
which allows you to get the immediate parent of an element -
method
parentsUntil,
which allows you to get all the parents of an element up to the specified parent -
method
closest,
which allows you to get the closest parent element that satisfies the selector -
method
contents,
which allows you to get the descendants of an element and the text inside that element