jQuery | find() with Examples

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    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.