jQuery | find() with Examples

03 Aug, 2021
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.
<html>  <head>    <style>        .descendants * {            display: block;            border: 2px solid grey;            color: lightgrey;            padding: 5px;            margin: 15px;        }    </style>    <script src="https://ajax.googleapis.com/ajax/libs/                 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>    <script src="https://ajax.googleapis.com/ajax/libs/                 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.
jQuery-Traversing
JavaScript
JQuery
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
How to append HTML code to a div using JavaScript ?
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to add options to a select element using jQuery?
How to fetch data from JSON file and display in HTML table using jQuery ?