jQuery -

Traversing

jQuery traversing methods allow you to move through the DOM tree, finding and filtering elements based on their relationships with other elements. This tutorial covers different traversing techniques with detailed explanations and examples.


1. Parent Element

The .parent() method returns the direct parent of the selected element.

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 get the direct parent of an element using .parent().



2. All Ancestor Elements

The .parents() method returns all ancestor elements of the selected element, up to the document's root element.

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 get all ancestor elements of an element using .parents().


3. Specific Ancestor Elements

The .parentsUntil() method returns all ancestor elements between the selected element and the specified selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .parentsUntil() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#parentsUntilButton").click(function(){
                $("span").parentsUntil("div").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="parentsUntilButton">Get Ancestors Until Div</button>
</body>
</html>
        
    

This example shows how to get all ancestors up to, but not including, a specific element using .parentsUntil().



4. Child Elements

The .children() method returns all direct children of the selected element.

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

This example demonstrates how to get the direct children of an element using .children().


5. Descendant Elements

The .find() method returns all descendant elements of the selected element that match the specified selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#findButton").click(function(){
                $("div").find("span").each(function(){
                    alert($(this).text());
                });
            });
        });
    </script>
</head>
<body>
    <div><span>This is a span inside a div.</span><p>This is a paragraph inside a div.</p></div>
    <button id="findButton">Find Descendants</button>
</body>
</html>
        
    

This example shows how to find all descendants that match a selector using .find().



6. Sibling Elements

The .siblings() method returns all sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .siblings() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#siblingsButton").click(function(){
                $("p").siblings().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="siblingsButton">Get Siblings</button>
</body>
</html>
        
    

This example demonstrates how to get all siblings of an element using .siblings().


7. Next Sibling Element

The .next() method returns the next sibling element of the selected element.

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 get the next sibling element using .next().



8. Previous Sibling Element

The .prev() method returns the previous sibling element of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prev() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#prevButton").click(function(){
                alert($("span").prev().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="prevButton">Get Previous Sibling</button>
</body>
</html>
        
    

This example demonstrates how to get the previous sibling element using .prev().


9. All Next Sibling Elements

The .nextAll() method returns all next sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .nextAll() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#nextAllButton").click(function(){
                $("p").nextAll().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="nextAllButton">Get All Next Siblings</button>
</body>
</html>
        
    

This example shows how to get all next siblings using .nextAll().


10. All Previous Sibling Elements

The .prevAll() method returns all previous sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prevAll() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#prevAllButton").click(function(){
                $("h2").prevAll().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="prevAllButton">Get All Previous Siblings</button>
</body>
</html>
        
    

This example demonstrates how to get all previous siblings using .prevAll().



11. Next Sibling Elements Until

The .nextUntil() method returns all next sibling elements 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 get all next siblings up to a specified element using .nextUntil().


12. Previous Sibling Elements Until

The .prevUntil() method returns all previous sibling elements up to, but not including, the element matched by the selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prevUntil() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#prevUntilButton").click(function(){
                $("h2").prevUntil("p").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="prevUntilButton">Get Previous Siblings Until Paragraph</button>
</body>
</html>
        
    

This example demonstrates how to get all previous siblings up to a specified element using .prevUntil().


13. Closest Ancestor

The .closest() method returns the first ancestor of the selected element that matches the specified selector.

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

This example shows how to find the closest ancestor that matches a selector using .closest().



14. Filtering Elements

jQuery provides methods to filter elements, such as .filter(), .not(), and .is().

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .filter() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").filter(".highlight").css("color", "red");
            });
        });
    </script>
</head>
<body>
    <p class="highlight">This paragraph will be filtered.</p>
    <p>This paragraph will not be filtered.</p>
    <button>Filter Paragraphs</button>
</body>
</html>
        
    

This example shows how to filter elements that match a certain criteria using .filter().

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .not() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").not(".exclude").css("color", "blue");
            });
        });
    </script>
</head>
<body>
    <p class="exclude">This paragraph will be excluded.</p>
    <p>This paragraph will not be excluded.</p>
    <button>Exclude Paragraphs</button>
</body>
</html>
        
    

This example demonstrates how to exclude elements that match a certain criteria using .not().

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .is() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                if ($("p").is(".highlight")) {
                    alert("At least one paragraph has the 'highlight' class.");
                } else {
                    alert("No paragraph has the 'highlight' class.");
                }
            });
        });
    </script>
</head>
<body>
    <p class="highlight">This paragraph has the 'highlight' class.</p>
    <p>This paragraph does not have the 'highlight' class.</p>
    <button>Check for Class</button>
</body>
</html>
        
    

This example illustrates how to check if any of the selected elements match a certain criteria using .is().


15. End Method

The .end() method ends the most recent filtering operation in the current chain and returns the set of matched elements to its previous state.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .end() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").filter(".highlight").css("color", "red").end().css("background-color", "yellow");
            });
        });
    </script>
</head>
<body>
    <p class="highlight">This paragraph will be filtered and colored red, then all paragraphs will have a yellow background.</p>
    <p>This paragraph will only have a yellow background.</p>
    <button>End Filtering</button>
</body>
</html>
        
    

This example shows how to end a filtering operation and return to the previous state using .end().


16. First Element

The .first() method returns the first element in the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .first() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").first().css("color", "green");
            });
        });
    </script>
</head>
<body>
    <p>This is the first paragraph.</p>
    <p>This is the second paragraph.</p>
    <button>Color First Paragraph</button>
</body>
</html>
        
    

This example demonstrates how to get the first element in a set using .first().


17. Last Element

The .last() method returns the last element in the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .last() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").last().css("color", "purple");
            });
        });
    </script>
</head>
<body>
    <p>This is the first paragraph.</p>
    <p>This is the last paragraph.</p>
    <button>Color Last Paragraph</button>
</body>
</html>
        
    

This example shows how to get the last element in a set using .last().


18. Even Indexed Elements

The .even() method returns all elements with an even index in the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .even() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").even().css("color", "blue");
            });
        });
    </script>
</head>
<body>
    <p>This is paragraph 1.</p>
    <p>This is paragraph 2.</p>
    <p>This is paragraph 3.</p>
    <p>This is paragraph 4.</p>
    <button>Color Even Paragraphs</button>
</body>
</html>
        
    

This example demonstrates how to get all elements with an even index using .even().



19. Odd Indexed Elements

The .odd() method returns all elements with an odd index in the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .odd() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").odd().css("color", "red");
            });
        });
    </script>
</head>
<body>
    <p>This is paragraph 1.</p>
    <p>This is paragraph 2.</p>
    <p>This is paragraph 3.</p>
    <p>This is paragraph 4.</p>
    <button>Color Odd Paragraphs</button>
</body>
</html>
        
    

This example shows how to get all elements with an odd index using .odd().


20. Nth Element

The .eq() method returns the element at a specific index in the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .eq() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").eq(2).css("color", "orange");
            });
        });
    </script>
</head>
<body>
    <p>This is paragraph 1.</p>
    <p>This is paragraph 2.</p>
    <p>This is paragraph 3.</p>
    <p>This is paragraph 4.</p>
    <button>Color Third Paragraph</button>
</body>
</html>
        
    

This example demonstrates how to get the nth element in a set using .eq().



21. Traversing Up the DOM

The .closest() method returns the first ancestor of the selected element that matches the specified selector.

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

This example shows how to find the closest ancestor that matches a selector using .closest().


22. Filtering Siblings

The .siblings() method returns all sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .siblings() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#siblingsButton").click(function(){
                $("p").siblings().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="siblingsButton">Get Siblings</button>
</body>
</html>
        
    

This example demonstrates how to get all siblings of an element using .siblings().


23. All Following Siblings

The .nextAll() method returns all next sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .nextAll() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#nextAllButton").click(function(){
                $("p").nextAll().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="nextAllButton">Get All Next Siblings</button>
</body>
</html>
        
    

This example shows how to get all next siblings using .nextAll().


24. All Previous Siblings

The .prevAll() method returns all previous sibling elements of the selected element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prevAll() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#prevAllButton").click(function(){
                $("h2").prevAll().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="prevAllButton">Get All Previous Siblings</button>
</body>
</html>
        
    

This example demonstrates how to get all previous siblings using .prevAll().


25. Filtering Ancestors

The .parentsUntil() method returns all ancestor elements between the selected element and the specified selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .parentsUntil() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#parentsUntilButton").click(function(){
                $("span").parentsUntil("div").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="parentsUntilButton">Get Ancestors Until Div</button>
</body>
</html>
        
    

This example shows how to get all ancestors up to, but not including, a specific element using .parentsUntil().


26. All Children

The .children() method returns all direct children of the selected element.

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

This example demonstrates how to get the direct children of an element using .children().


27. Descendant Elements

The .find() method returns all descendant elements of the selected element that match the specified selector.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .find() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#findButton").click(function(){
                $("div").find("span").each(function(){
                    alert($(this).text());
                });
            });
        });
    </script>
</head>
<body>
    <div><span>This is a span inside a div.</span><p>This is a paragraph inside a div.</p></div>
    <button id="findButton">Find Descendants</button>
</body>
</html>
        
    

This example shows how to find all descendants that match a selector using .find().


28. Traversing by Index

The .eq() method returns the element at a specific index in the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .eq() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").eq(2).css("color", "orange");
            });
        });
    </script>
</head>
<body>
    <p>This is paragraph 1.</p>
    <p>This is paragraph 2.</p>
    <p>This is paragraph 3.</p>
    <p>This is paragraph 4.</p>
    <button>Color Third Paragraph</button>
</body>
</html>
        
    

This example demonstrates how to get the nth element in a set using .eq().


29. Filtering by Class

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</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").filter(".highlight").css("color", "red");
            });
        });
    </script>
</head>
<body>
    <p class="highlight">This paragraph will be filtered.</p>
    <p>This paragraph will not be filtered.</p>
    <button>Filter Paragraphs</button>
</body>
</html>
        
    

This example shows how to filter elements that match a certain criteria using .filter().


30. Filtering by Exclusion

The .not() method removes elements that match the selector from the set of matched elements.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .not() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $("p").not(".exclude").css("color", "blue");
            });
        });
    </script>
</head>
<body>
    <p class="exclude">This paragraph will be excluded.</p>
    <p>This paragraph will not be excluded.</p>
    <button>Exclude Paragraphs</button>
</body>
</html>
        
    

This example demonstrates how to exclude elements that match a certain criteria using .not().