JQUERY BASIC
JQUERY EFFECTS
JQUERY MANIPULATION
JQUERY ADVANCED
JQUERY EXAMPLES
JQUERY REFERENCE

jQuery Traversing Ancestors

In this tutorial you will learn how to traversing up the DOM tree using jQuery.

Traversing Up the DOM Tree

In logical relationships an ancestor is a parent, grandparent, great-grandparent, and so on.

jQuery provides the useful methods such as parent(), parents() and parentsUntil() that you can use to traverse up in the DOM tree either single or multiple levels to easily get the parent or other ancestors of an element in the hierarchy.

jQuery parent() Method

The jQuery parent() method is used to get the direct parent of the selected element.

The following example will highlight the direct parent element of the <li> which is <ul> by adding the class .highlight on document ready.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery parent() Demo</title>
  • <style type="text/css">
  •     .highlight{
  •         background: yellow;
  •     }        
  • </style>
  • <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  • <script type="text/javascript">
  • $(document).ready(function(){
  •     $("li").parent().addClass("highlight");
  • });
  • </script>
  • </head>
  • <body>
  •     <div class="container">
  •         <h1>Hello World</h1>
  •         <p>This is a <em>simple paragraph</em>.</p>
  •         <ul>
  •             <li>Item One</li>
  •             <li>Item Two</li>
  •         </ul>
  •     </div>
  • </body>
  • </html>

jQuery parents() Method

The jQuery parents() method is used to get the ancestors of the selected element.

The following example will add a border around all the ancestor elements of the <li> which are <ul>, <div>, <body> and the <html> elements.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery parents() Demo</title>
  • <style type="text/css">
  •     *{
  •         margin: 10px;
  •     }
  •     .frame{
  •         border: 2px solid green;
  •     }        
  • </style>
  • <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  • <script type="text/javascript">
  • $(document).ready(function(){
  •     $("li").parents().addClass("frame");
  • });
  • </script>
  • </head>
  • <body>
  •     <div class="container">
  •         <h1>Hello World</h1>
  •         <p>This is a <em>simple paragraph</em>.</p>
  •         <ul>
  •             <li>Item One</li>
  •             <li>Item Two</li>
  •         </ul>
  •     </div>
  • </body>
  • </html>

You can optionally include one or more selector as a parameter within the parents() method to filter your search for the ancestors. The following example will apply the border around all the ancestors of the <li> that are <div> elements.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery parents() Demo</title>
  • <style type="text/css">
  •     *{
  •         margin: 10px;
  •     }
  •     .frame{
  •         border: 2px solid green;
  •     }        
  • </style>
  • <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  • <script type="text/javascript">
  • $(document).ready(function(){
  •     var arr = $("li").parents("div").addClass("frame");
  • });
  • </script>
  • </head>
  • <body>
  •     <div class="container">
  •         <h1>Hello World</h1>
  •         <p>This is a <em>simple paragraph</em>.</p>
  •         <ul>
  •             <li>Item One</li>
  •             <li>Item Two</li>
  •         </ul>
  •     </div>
  • </body>
  • </html>

jQuery parentsUntil() Method

The jQuery parentsUntil() method is used to get all the ancestors up to but not including the element matched by the selector. In simple words we can say it returns all ancestor elements between two given elements in a DOM hierarchy.

The following example will add a border around all the ancestor elements of the <li> excluding <html> element i.e. add a border to <ul>, <div> and <body> element.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="utf-8">
  • <title>jQuery parentsUntil() Demo</title>
  • <style type="text/css">
  •     *{
  •         margin: 10px;
  •     }
  •     .frame{
  •         border: 2px solid green;
  •     }        
  • </style>
  • <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  • <script type="text/javascript">
  • $(document).ready(function(){
  •     $("li").parentsUntil("html").addClass("frame");
  • });
  • </script>
  • </head>
  • <body>
  •     <div class="container">
  •         <h1>Hello World</h1>
  •         <p>This is a <em>simple paragraph</em>.</p>
  •         <ul>
  •             <li>Item One</li>
  •             <li>Item Two</li>
  •         </ul>
  •     </div>
  • </body>
  • </html>

Your Feedback:

We would love to hear from you, please drop us a line.