Skip to content
Related Articles

Related Articles

Improve Article

How to select specific ancestors of an element in jQuery ?

  • Last Updated : 24 Mar, 2021
Geek Week

In this article, we will select specific ancestor elements of an element in jQuery. To select the specific ancestor elements of an element, we use parents() method. This method is used to find the parent elements related to the selected element. This parents() method traverse all the levels up the selected element and return that all elements.

Syntax:

$(selector).parents()

Here selector is the selected element whose all parent need to find. It returns all the parents of the selected element.

Example:

HTML




<!DOCTYpe html>
<html>
  
<head>
    <title>
        How to select specific ancestor 
        elements of an element in jQuery?
    </title>
  
    <script src=
    </script>
  
    <script>
        $(document).ready(function () {
            $("li").parents("ul").css({
                color: "green",
                border: "2px solid green"
            });
        });
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to select specific ancestor 
        elements of an element in jQuery?
    </h3>
  
    <p>GeeksforGeeks Subjects List-</p>
  
    <ul>
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
        <li>PHP</li>
    </ul>
</body>
  
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :