Open In App

jQuery find() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The find() is an inbuilt method in jQuery which is used to find all the descendant elements of the selected element. It will traverse all the way down to the last leaf of the selected element in the DOM tree.
Syntax:

$(selector).find()

Here selector is the selected elements of which all the descendant elements are going to be found.
Parameters: It does not accept any parameter.

Return Value: It returns all the descendant elements of the selected element.

jQuery code to show the working of this function:

Code #1:
In the below code, all the “span” element connected to the “div” element get highlighted with the green color.




<html>
  
<head>
    <style>
        .descendants * {
            display: block;
            border: 2px solid grey;
            color: lightgrey;
            padding: 5px;
            margin: 15px;
        }
    </style>
                 jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("div").find("span").css({
                "color": "green",
                "border": "2px solid green"
            });
        });
    </script>
</head>
  
<body>
  
    <div class="descendants"
         style="width:500px;">This is the current element !!!
        <p>This is the paragraph element !!!
            <span> This is span element !!!</span>
        </p>
        <p>This is the paragraph element !!!
            <span>This is span element !!!</span>
        </p>
    </div>
  
</body>
  
</html>


Output:

All the descendant element of the particular element can also be found with the help of find() function with some parameter.
Syntax:

$(selector1).children("selector2")

Here selector1 is the selected element whose all the descendant element are going to be found.

Parameters: It accepts a parameter which is specified below-

  • selector2: This is the just “*” sign which return all the children of the selected element.
  • Return value: It returns all the children of the selected element.
    Code #2:
    In the below code, all the “span” elements of the “p” element gets selected and highlighted with green color.




    <html>
      
    <head>
        <style>
            .descendants * {
                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() {
                $("p").find("*").css({
                    "color": "green",
                    "border": "2px solid green"
                });
            });
        </script>
    </head>
      
    <body>
      
        <div class="descendants" 
             style="width:500px;">This is the current element !
            <p>This is the paragraph element !!!!
                <span>This is span 1 !!!</span>
                <span>This is span 2 !!!</span>
                <span>This is span 3 !!!</span>
                <span>This is span 4 !!!</span>
            </p>
        </div>
      
    </body>
      
    </html>

    
    

    Output:

    jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
    You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.



    Last Updated : 04 Jul, 2023
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads