jQuery .parentsUntil() gets the ancestors of each element up to, but not including
Syntax and Description
.parentsUntil(selector)
Get the ancestors of each element up to, but not including, the element matched by the selector.
selector
is a string containing a selector expression to indicate
where to stop matching ancestor elements.
The return value is the new jQuery object.
For the following HTML tags,
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>/*from www . j a va2 s . c om*/
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
$('li.item-a').parentsUntil('.level-1').css('background-color', 'red');
marks level-2 list and the item II to red.
If the .parentsUntil()
selector is not
matched, or if no selector is supplied, the
returned jQuery object contains all of the previous
jQuery object's ancestors.