jQuery | nextUntil() with Examples

The nextUntil() is an inbuilt method in jQuery which is used to find all sibling elements between two given elements. Siblings are those having same parent element in DOM Tree. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree.
Syntax:

$(selector1).nextUntil(selector2)

Here selector1 is the starting element after which siblings are going to be found out.
Parameters: It accepts a parameter “selector2” which is the last selected element to find the siblings.
Return Value: It returns all the siblings between “selector1” and “selector2”.

jQuery code to show the working of nextUntil() method:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <style>
        .bet_sib * {
            display: block;
            border: 2px solid lightgrey;
            color: black;
            padding: 5px;
            margin: 15px;
        }
    </style>
                jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("span").nextUntil("p").css({
                "color": "black",
                "border": "2px solid green"
            });
        });
    </script>
</head>
  
<body class="bet_sib">
    <div>
        This is the parent element !!!
        <p>This is first paragraph!</p>
        <span>first span box !</span>
        <h2>heading 2!</h2>
        <h3>heading 3!</h3>
        <h4>heading 4!</h4>
        <h5>heading 5!</h5>
        <h6>heading 6!</h6>
        <p>This is second paragraph!</p>
    </div>
</body>
  
</html>

chevron_right


In the above code, all the elements (or siblings) between “span” and next “p” get highlighted.
Output:

Code #2:
In the below code, all the siblings between same pair of the elements can be selected.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <style>
        .bet_sib * {
            display: block;
            border: 2px solid lightgrey;
            color: black;
            padding: 5px;
            margin: 15px;
        }
    </style>
                 jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("p").nextUntil("p").css({
                "color": "black",
                "border": "2px solid green"
            });
        });
    </script>
</head>
  
<body class="bet_sib">
    <div>
        This is the parent element !!!
        <p>This is first paragraph!</p>
        <span>first span box !</span>
        <h2>heading 2!</h2>
        <h3>heading 3!</h3>
        <h4>heading 4!</h4>
        <h5>heading 5!</h5>
        <h6>heading 6!</h6>
        <p>This is second paragraph!</p>
    </div>
</body>
  
</html>

chevron_right


In the above code, all the elements (or siblings) between the paragraph elements get highlighted with green color.
Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.