jQuery -

Descendants


What are jQuery Descendants?

In jQuery, the term "descendants" refers to all the elements that are nested within a specified parent element. jQuery provides several methods to traverse down the DOM tree and select descendant elements. This tutorial covers the basics and advanced concepts of using jQuery methods to select descendants with detailed examples and useful tips.


1. Basic Descendants

You can use the .find() method to get all descendant elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Basic</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid red");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
    </div>
    <button id="btn">Highlight Descendants</button>
</body>
</html>
        
    

This example demonstrates how to use the .find() method to select all descendant elements of a div element.


2. Specific Descendants

You can use the .children() method to get the immediate children of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .children() Method - Specific Descendants</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").children("p").css("border", "2px solid blue");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
    </div>
    <button id="btn">Highlight Specific Descendants</button>
</body>
</html>
        
    

This example shows how to use the .children() method to select the immediate children of a div element.


3. Filtering Descendants

You can filter the descendant elements using the .find() method with a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Filtering Descendants</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid green");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Highlight Filtered Descendants</button>
</body>
</html>
        
    

This example demonstrates how to filter the descendants of a div element using a selector.


4. Descendants by Class

You can select descendants of a specific class using the .find() method with a class selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Descendants by Class</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find(".highlight").css("border", "2px solid orange");
            });
        });
    </script>
</head>
<body>
    <div>
        <p class="highlight">This is a highlighted paragraph.</p>
        <p>This is another paragraph.</p>
    </div>
    <button id="btn">Highlight Descendants by Class</button>
</body>
</html>
        
    

This example shows how to select descendants of a specific class within a div element.


5. Descendants by Tag Name

You can select descendants of a specific tag name using the .find() method with a tag selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Descendants by Tag Name</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid purple");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Highlight Descendants by Tag Name</button>
</body>
</html>
        
    

This example demonstrates how to select descendants of a specific tag name within a div element.


6. Descendants by Attribute

You can select descendants with a specific attribute using the .find() method with an attribute selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Descendants by Attribute</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("[data-highlight]").css("border", "2px solid pink");
            });
        });
    </script>
</head>
<body>
    <div>
        <p data-highlight="true">This is a highlighted paragraph.</p>
        <p>This is another paragraph.</p>
    </div>
    <button id="btn">Highlight Descendants by Attribute</button>
</body>
</html>
        
    

This example shows how to select descendants with a specific attribute within a div element.


7. Combining Descendants with Other Methods

You can combine descendant methods with other jQuery methods to create complex selections and manipulations.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Combining with CSS</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css({"border": "2px solid teal", "background-color": "lightgray"});
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Highlight and Style Descendants</button>
</body>
</html>
        
    

This example demonstrates how to combine the .find() method with the .css() method to style all descendant elements.



8. Descendants with Event Handling

You can use descendant methods within event handlers to target specific descendants when an event occurs.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Event Handling</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $(this).find("p").css("border", "2px solid lime");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <button id="btn">Highlight Descendants on Click</button>
    </div>
</body>
</html>
        
    

This example shows how to use the .find() method in a click event handler to style all descendant elements of the clicked element.


9. Descendants with Siblings

You can combine descendant methods with sibling methods to select elements in a more refined way.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Siblings</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $(this).find("p").siblings().css("border", "2px solid brown");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
        <button id="btn">Highlight Sibling Descendants</button>
    </div>
</body>
</html>
        
    

This example demonstrates how to use the .find() method with the .siblings() method to select all sibling elements of the descendants.


10. Multiple Descendants

You can select multiple descendants by chaining descendant methods.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Multiple Descendants</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid red").children().css("background-color", "lightblue");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Highlight Multiple Descendants</button>
</body>
</html>
        
    

This example shows how to chain the .find() and .children() methods to select multiple descendants.



11. Descendants and Ancestors

You can use descendant methods in combination with ancestor methods to traverse both up and down the DOM tree.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Descendants and Ancestors</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid pink").parents().css("background-color", "lightgreen");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Highlight Descendants and Ancestors</button>
</body>
</html>
        
    

This example demonstrates how to use the .find() method with the .parents() method to select specific elements.


12. Best Practices with Descendants

Following best practices when using descendant methods ensures efficient and maintainable code.


13. Descendants with Custom Functions

You can define custom functions and use them with descendant methods to create reusable and modular code.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Custom Functions</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function highlightDescendants(element) {
            element.find("p").css("border", "2px solid purple");
        }

        $(document).ready(function(){
            $("#btn").click(function(){
                highlightDescendants($("div"));
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Highlight Descendants with Custom Function</button>
</body>
</html>
        
    

This example demonstrates how to create a custom function and use it with descendant methods to manipulate elements.


14. Descendants with Ajax

You can use descendant methods with Ajax calls to update specific parts of the DOM after an Ajax request.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Ajax</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#ajaxButton").click(function(){
                $.ajax({
                    url: "https://jsonplaceholder.typicode.com/posts/1",
                    success: function(result){
                        $("div").html("<p>" + result.title + "</p>").find("p").css("border", "2px solid black");
                    }
                });
            });
        });
    </script>
</head>
<body>
    <button id="ajaxButton">Load Data and Highlight</button>
    <div></div>
</body>
</html>
        
    

This example shows how to use descendant methods to update elements within a div after an Ajax request.


15. Combining Descendants and Traversing Methods

You can combine descendant methods with other traversing methods to create complex DOM manipulations.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method - Combining Traversing Methods</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("div").find("p").css("border", "2px solid red").end().children().css("background-color", "lightblue");
            });
        });
    </script>
</head>
<body>
    <div>
        <p>This is a paragraph.</p>
        <p>Another paragraph.</p>
    </div>
    <button id="btn">Combine Traversing Methods</button>
</body>
</html>
        
    

This example demonstrates how to use the .find() method with other traversing methods to select and manipulate specific elements.