jQuery parentsUntil()Method
Example
Return all ancestor elements between <span> and <div>:
$("span").parentsUntil("div").css({"color":"red","border":"2px solid red"});
});
Result:
- ul (grandparent)
- li (direct parent) span
Try it Yourself »
Definition and Usage
The parentsUntil() method returns betweenselectorandstopall ancestor elements between.
Ancestors are parent, grandparent, great-grandparent, and so on.
DOM tree:This method traverses the ancestors of DOM elements upwards from the parent element, along all paths to the document root element, until it reaches the specified element.
Note:If both parameters are empty, this method will return all ancestor elements (same as the parents()method).
Related methods:
- parent()- Returns the direct parent element of the selected element
- parents()- Returns all ancestor elements of the selected element
- closest()- Returns the first ancestor of the selected element
Syntax
| Parameter | Description |
|---|---|
| stop | Optional. A selector expression, element, or jQuery object indicating where to stop searching for matching ancestor elements. |
| filter | Optional. Specifies a selector expression that narrows the search betweenselectorandstopthe range of ancestor elements between. Note:To return multiple ancestor elements, use a comma to separate each expression. |
More Examples
Narrow the search range
How to use two parameters to filter the search for specified elements between <span> and <div>.
Return multiple ancestor elements
How to return multiple ancestor elements between <span> and <body>.
DOM
Use DOM elements to return all ancestor elements between <span> and <div>.
Use DOM elements and selector expressions to filter the search
Use DOM elements to narrow the search for ancestor elements <ul> between <span> and <div>.
Other Extensions
jQuery Traversal Methods