jQuery -

Siblings


What are jQuery Siblings?

In jQuery, the term "siblings" refers to elements that share the same parent. jQuery provides several methods to traverse sideways in the DOM tree and select sibling elements. This tutorial covers the basics and advanced concepts of using jQuery methods to select siblings with detailed examples and useful tips.


1. Basic Siblings

You can use the .siblings() method to get all sibling elements of the selected element.

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

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


2. Specific Siblings

You can use the .siblings() method with a selector to filter the siblings.

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

This example shows how to use the .siblings() method with a selector to select specific siblings.


3. Next Sibling

You can use the .next() method to get the immediately following sibling.

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

This example demonstrates how to use the .next() method to select the next sibling of a div element.


4. Previous Sibling

You can use the .prev() method to get the immediately preceding sibling.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prev() Method - 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>
    <div>This is a div element.</div>
    <p>This is a paragraph.</p>
    <button id="btn">Highlight Previous Sibling</button>
</body>
</html>
        
    

This example shows how to use the .prev() method to select the previous sibling of a div element.


5. All Next Siblings

You can use the .nextAll() method to get all following siblings.

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

This example demonstrates how to use the .nextAll() method to select all next siblings of a div element.


6. All Previous Siblings

You can use the .prevAll() method to get all preceding siblings.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .prevAll() Method - 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 brown");
            });
        });
    </script>
</head>
<body>
    <div>This is a div element.</div>
    <p>This is a paragraph.</p>
    <p>Another paragraph.</p>
    <button id="btn">Highlight All Previous Siblings</button>
</body>
</html>
        
    

This example shows how to use the .prevAll() method to select all previous siblings of a div element.


7. Next Until

You can use the .nextUntil() method to get 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 demonstrates how to use the .nextUntil() method to select all next siblings until a specific element.


8. Previous Until

You can use the .prevUntil() method to get 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 shows how to use the .prevUntil() method to select all previous siblings until a specific element.



9. Filtering Siblings

You can filter siblings using the .filter() method.

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

This example demonstrates how to filter siblings using a class selector.


10. Combining Sibling Methods

You can combine sibling methods to create complex selections and manipulations.

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

This example shows how to combine the .next() and .prev() methods to select and style sibling elements.


11. Siblings with Event Handling

You can use sibling methods within event handlers to target specific siblings when an event occurs.

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

This example demonstrates how to use the .siblings() method in a click event handler to style all sibling elements of the clicked element.


12. Siblings and Traversing

You can use sibling methods in combination with other traversing methods to navigate the DOM tree.

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

This example shows how to use the .siblings() method with the .find() method to select specific elements.


13. Siblings and Ancestors

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

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

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



14. Siblings with Custom Functions

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

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

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

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


15. Siblings with Ajax

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

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .siblings() 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>").siblings().css("border", "2px solid black");
                    }
                });
            });
        });
    </script>
</head>
<body>
    <button id="ajaxButton">Load Data and Highlight Siblings</button>
    <div></div>
    <p>This is a paragraph.</p>
</body>
</html>
        
    

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


16. Best Practices with Siblings

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