jQuery | closest() with Examples

The closest() is an inbuilt method in jQuery that returns the first ancestor of the selected element in the DOM tree. This method traverse upwards from the current element in the search of first ancestor of the element. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree.
Syntax:

$(selector).closest(para1, para2);

Parameter: It accepts two parameters which are specified below-

  • para1: This specifies the element to narrate the ancestor search in the DOM tree.
  • para2: This is an optional parameter DOM element within which a matching element being found.

Return Value: It returns the first ancestor for the selected element.



jQuery code to show the working of closest() method:

Code #1:
In the below code, optional parameter is not passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <style>
        .main * {
            display: block;
            border: 2px solid lightgrey;
            color: grey;
            padding: 5px;
            margin: 15px;
        }
    </style>
               jquery/3.3.1/jquery.min.js"></script>
    <script>
    <!-- here is the script code for performing the method -->
        $(document).ready(function() {
            $("span").closest("ul").css({
                "color": "green",
                "border": "2px solid green"
            });
        });
    </script>
</head>
  
<body class="main">
    This is great-great grand parent element !
    <div style="width:600px;">
        This is great grand parent element !
        <ul>
            This is the second ancestor element !
            <ul>
                <!-- This element will be selected -->
                This is first ancestor element !
                <li>This is direct parent !
                <span>This is span the child element !</span>
                </li>
            </ul>
        </ul>
    </div>
</body>
  
</html>

chevron_right


Output:

Code #2:
In the below code, optional parameter is passed to the method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <style>
        .main * {
            display: block;
            border: 2px solid lightgrey;
            color: grey;
            padding: 5px;
            margin: 15px;
        }
    </style>
             jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
        <!--Here among dom id first ancestor will select -->
            var item = document.getElementById("dom");
            $("li").closest("ul", item).css({
                "color": "green",
                "border": "2px solid green"
            });
        });
    </script>
</head>
  
<body class="main">
    This is great-great-grandparent !
    <div style="width:500px;">
        div (great-grandparent)
        <ul id="dom">
            This is second ancestor !
            <ul id="dom">
                This is first ancestor !
                <li>This is direct parent !
                <span>This is span the child one !</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.