.parentsUntil()
methodThe .parentsUntil()
method in jQuery is used to get the ancestors of each element in the set of matched elements level by level until meeting the element matched by the selector ,DOM node, or jQuery object.
.parentsUntil()
method Syntax.parentsUntil([selector][,filter]) .parentsUntil([element][,filter])
Parameter | Type | Description |
---|---|---|
selector |
Selector | selector expression to indicate where to stop matching following sibling elements |
element |
Element, jQuery | DOM element or jQuery object indicating where to stop matching following sibling elements |
filter |
Selector | selector expression to match elements against |
.parentsUntil()
method ExampleFind the ancestors of <li id="python">
up to <li class="category">
.
HTML
<ul> <li class="category">Language <ul> <li class="lang">Java <ul> <li>Java tutorial</li> <li>Java Examples</li> </ul> </li> <li class="lang">Python <ul id="python"> <li>Python tutorial</li> <li id="pyex">Python Examples</li> </ul> </li> </ul> </li> <li class="category">Tools</li> <li class="category">Lautturi</li> </ul>
jQuery
var parentElems = $( "#python" ).parentsUntil('.category') .map(function(){ return this.tagName; }) .get() .join(", "); $('#log').html("The matched ancestors are:"+ parentElems);
Result
The `#python` element's parents are:LI, UL, LI, UL, BODY, HTML
Find the ancestors of <li id="pyex">
that have tag name ul
up to <li class="category">
.
$( "#pyex" ).parentsUntil('.category','ul').css( "border", "1px solid red" );