jQuery -

Traversal Methods


Introduction to jQuery Traversal Methods

jQuery traversal methods allow you to navigate through the DOM tree to find and manipulate elements. These methods provide a powerful way to access and manipulate elements based on their relationships to other elements. This tutorial covers the most commonly used jQuery traversal methods with detailed examples, explanations, and useful tips.


1. Finding Elements with .find()

The .find() method is used to get all descendant elements of the selected element that match the selector.

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">Find and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to use the .find() method to select all paragraph elements within a div.


2. Filtering Elements with .filter()

The .filter() method reduces the set of matched elements to those that match the selector or pass the function's test.

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

This example shows how to use the .filter() method to select elements that match a specific class.


3. Getting Parent Elements with .parent()

The .parent() method gets the immediate parent of each element in the current set of matched elements.

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

This example demonstrates how to find and style the parent element of a selected element.


4. Getting Ancestors with .parents()

The .parents() method gets all ancestors of each element in the current set of matched elements, optionally filtered by a selector.

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

This example shows how to use the .parents() method to find all ancestor elements of a selected element.


5. Getting Closest Ancestor with .closest()

The .closest() method gets the first ancestor of each element in the current set of matched elements, optionally filtered by a selector.

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

This example demonstrates how to use the .closest() method to find the closest ancestor element of a selected element.


6. Getting Child Elements with .children()

The .children() method gets the immediate children of each element in the set of matched elements, optionally filtered by a selector.

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

This example shows how to use the .children() method to find and manipulate the children of a selected element.


7. Getting Sibling Elements with .siblings()

The .siblings() method gets all sibling elements of each element in the set of matched elements, optionally filtered by a selector.

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

This example demonstrates how to find and style the siblings of a selected element.


8. Getting Next Sibling with .next()

The .next() method gets the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector.

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

This example shows how to find and manipulate the next sibling of a selected element.


9. Getting All Next Siblings with .nextAll()

The .nextAll() method gets all following siblings of each element in the set of matched elements, optionally filtered by a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .nextAll() Method - Finding All Next Siblings</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").nextAll().css("border", "2px solid green");
            });
        });
    </script>
</head>
<body>
    <p>This is a paragraph.</p>
    <p>This is the next sibling paragraph.</p>
    <p>This is another next sibling paragraph.</p>
    <button id="btn">Find All Next Siblings and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and style all next siblings of a selected element.


10. Getting Previous Sibling with .prev()

The .prev() method gets the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.

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

This example shows how to find and manipulate the previous sibling of a selected element.


11. Getting All Previous Siblings with .prevAll()

The .prevAll() method gets all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prevAll() Method - Finding All Previous Siblings</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").prevAll().css("border", "2px solid purple");
            });
        });
    </script>
</head>
<body>
    <p>This is another previous sibling paragraph.</p>
    <p>This is the previous sibling paragraph.</p>
    <p>This is a paragraph.</p>
    <button id="btn">Find All Previous Siblings and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and style all previous siblings of a selected element.


12. Getting Elements Until a Selector with .nextUntil()

The .nextUntil() method gets all following siblings up to but not including the element matched by the selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .nextUntil() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#nextUntilButton").click(function(){
                $("p").nextUntil("h2").each(function(){
                    alert($(this).prop("tagName"));
                });
            });
        });
    </script>
</head>
<body>
    <div><p>This is a paragraph.</p><span>This is a span.</span><h2>This is a heading.</h2></div>
    <button id="nextUntilButton">Get Next Siblings Until Heading</button>
</body>
</html>
        
    

This example shows how to find all next siblings of a selected element until a specific element.


13. Getting Elements Until a Selector with .prevUntil()

The .prevUntil() method gets all preceding siblings up to but not including the element matched by the selector.

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

This example demonstrates how to find all previous siblings of a selected element until a specific element.


14. Getting Immediate Next Sibling with .next()

The .next() method gets the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .next() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#nextButton").click(function(){
                alert($("p").next().prop("tagName"));
            });
        });
    </script>
</head>
<body>
    <div><p>This is a paragraph.</p><span>This is a span.</span><h2>This is a heading.</h2></div>
    <button id="nextButton">Get Next Sibling</button>
</body>
</html>
        
    

This example shows how to find and style the next sibling of a selected element.


15. Getting Immediate Previous Sibling with .prev()

The .prev() method gets the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prev() Method - Getting Immediate Previous Sibling</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#btn").click(function(){
                $("p").prev().css("border", "2px solid orange");
            });
        });
    </script>
</head>
<body>
    <p>This is the previous sibling paragraph.</p>
    <p>This is a paragraph.</p>
    <button id="btn">Find Previous and Highlight</button>
</body>
</html>
        
    

This example demonstrates how to find and manipulate the previous sibling of a selected element.


16. Getting Immediate Parent with .parent()

The .parent() method gets the immediate parent of each element in the set of matched elements, optionally filtered by a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .parent() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#parentButton").click(function(){
                alert($("span").parent().prop("tagName"));
            });
        });
    </script>
</head>
<body>
    <div><span>This is a span inside a div.</span></div>
    <button id="parentButton">Get Parent</button>
</body>
</html>
        
    

This example shows how to find and style the parent element of a selected element.


17. Getting All Ancestors with .parents()

The .parents() method gets all ancestors of each element in the set of matched elements, optionally filtered by a selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .parents() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#parentsButton").click(function(){
                $("span").parents().each(function(){
                    alert($(this).prop("tagName"));
                });
            });
        });
    </script>
</head>
<body>
    <div><section><span>This is a span inside a section inside a div.</span></section></div>
    <button id="parentsButton">Get All Ancestors</button>
</body>
</html>
        
    

This example demonstrates how to find all ancestor elements of a selected element.


18. Best Practices with Traversal Methods

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