jQuery | Traversing Ancestors

It is used to find ancestors of an element in the DOM tree.

There are different Methods for traversing up the DOM tree :

  • parent(): It is used to return the direct parent element of the given selected element.
  • parents(): It is used to return all ancestor elements of the given selected element upto root element.
  • parentsUntil(): It is used to return all the ancestor elements between two given arguments.

Syntax:



$(document).ready(function(){
  $("span").parent().css({"color": " ", "border": " "});
});

Example-1: Showing parent() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        .ancestors * {
            display: block;
            border: 2px solid lightgreen;
            color: green;
            padding: 10px;
            margin: 5px;
        }
    </style>
  
    <script src=
    </script>
    <script>
        $(document).ready(function() {
            $("span").parent().css({
                "color": "green",
                "border": "4px solid black"
            });
        });
    </script>
</head>
  
<body class="ancestors">great-great-grandparent
    <div style="width:500px;">great-grandparent
        <ul>grandparent
            <ul>direct parent
                <span>
                  <h1>
                    <li>
                    <center>
                      GeeksforGeeks
                    </center
                    </li
                 </h1>
              </span>
            </ul>
        </ul>
    </div>
</body>
  
</html>

chevron_right


Output:

Example-2: Showing parents() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        .ancestors * {
            display: block;
            border: 2px solid lightgreen;
            color: green;
            padding: 10px;
            margin: 5px;
        }
    </style>
  
    <script src=
    </script>
    <script>
        $(document).ready(function() {
            $("span").parents().css({
                "color": "green",
                "border": "4px solid black"
            });
        });
    </script>
</head>
  
<body class="ancestors">great-great-grandparent
    <div style="width:500px;">great-grandparent
        <ul>grandparent
            <ul>direct parent
                <li>
                    <span>
                  <h1>
                    <center>
                      GeeksforGeeks
                    </center>
                  </h1>
              </span>
                </li>
            </ul>
        </ul>
    </div>
</body>
  
</html>

chevron_right


Output:

Example-3: Showing parentsUntil() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        .ancestors * {
            display: block;
            border: 2px solid lightgreen;
            color: green;
            padding: 10px;
            margin: 5px;
        }
    </style>
  
    <script src=
    </script>
    <script>
        $(document).ready(function() {
            $("span").parentsUntil().css({
                "color": "green",
                "border": "4px solid black"
            });
        });
    </script>
</head>
  
<body class="ancestors">great-great-grandparent
    <div style="width:500px;">great-grandparent
        <ul>grandparent
            <ul>direct parent
                <li>
                    <span>
                  <h1>
                    <center>
                      GeeksforGeeks
                    </center>
                  </h1>
              </span>
                </li>
            </ul>
        </ul>
    </div>
</body>
  
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.