The parentsUntil method
The parentsUntil 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). The search for parents ends when the parent is reached that falls under the selector specified by the parameter of this method.
Syntax
Searches the parent until the parent matches the selector, the selector can be a DOM node, an expression or a jQuery object, and you can also use the second parameter for additional filtering. Both parameters are optional:
.parentsUntil([selector], [filter strainer percolator]);
The selector can be omitted, in which case the method will behave similarly to the parents method:
.parentsUntil();
Example
Let's find the element #test then find all its parents up to the element with class zzz using parentsUntil and prepend them with the text '!' using prepend:
<div>
<div class="zzz">
<div class="www">
<div class="www">
<p id="test">text</p>
</div>
</div>
</div>
</div>
$('#test').parentsUntil('.zzz').prepend('!');
HTML the code will look like this:
<div>
<div class="zzz">
<div class="www">
!
<div class="www">
!<p id="test">text</p>
</div>
</div>
</div>
</div>
See also
-
method
parent,
which allows you to get the immediate parent of an element -
method
closest,
which allows you to get the closest parent element that satisfies the selector -
method
parents,
which allows you to get all the parents of an element -
method
children,
which allows you to get the descendants of an element