The parentsUntil() is an inbuilt method in jQuery that is used to find all the ancestor elements between two given elements in a DOM tree. The Document Object Model(DOM) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree.
Syntax:
$(selector1).parentsUntil(selector2)
Parameters: It accepts a parameter “selector2” which is the last marked parent of the selected element in the tree.
Return Value: It returns all ancestors elements between two given elements.
jQuery code to show the working of parentsUntil() method:
<html> <head> <style> .anc * { display: block; border: 2px solid lightgrey; color: black; padding: 5px; margin: 15px; } </style> jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("span").parentsUntil("div").css({ "color": "black", "border": "2px solid green" }); }); </script> </head> <body class="anc"> This is great-great-grandparent ! <div style="width:400px;"> This is great-grandfather element ! <ul> This is grandparent ! <li>This is direct parent element ! <span>This is child element</span> </li> </ul> </div> </body> </html> |
In the above code, all the ancestor elements between “span” and “div” get highlighted with green color.
Output:

Recommended Posts:
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | add() method with Example
- jQuery | event.timeStamp property with Example
- jQuery | focusout() with Example
- jQuery | event.relatedTarget Property with Example
- jQuery | event.target Property with Example
- jQuery | innerWidth() with Example
- jQuery | finish() with Example
- jQuery | offsetParent() with Example
- jQuery | animated Selector with Example
- jQuery | :has() Selector with example
- JQuery | Multiple ID selectors
- jQuery | :input Selector
- jQuery | prepend() with Examples
- jQuery | dblclick() with Examples
- jQuery | offset() with Examples
- How to find the parent class name with known class in jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


