Open In App
Related Articles

jQuery prevUntil() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The prevUntil() is an inbuilt method in jQuery that is used to find all the previous 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 accessing elements in the DOM tree.

Syntax:

$(selector1).nextUntil(selector2)

Parameters: It accepts a parameter “selector2” which is the last selected element before which siblings are going to be found.

Return Value: It returns all the siblings between “selector1” and “selector2”.

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

Example 1: In this example, we are using the above-explained method.

html

<!DOCTYPE html>
<html>
 
<head>
    <style>
        .sib * {
            display: block;
            border: 2px solid lightgrey;
            color: black;
            padding: 5px;
            margin: 15px;
        }
    </style>
    <script src=
 
    </script>
    <script>
        $(document).ready(function () {
            $("li.start").prevUntil("li.stop").css({
                "color": "black",
                "border": "2px solid green"
            });
        });
    </script>
</head>
 
<body>
    <div style="width:400px;" class="sib">
        <ul>
            This is parent !!!
            <li class="stop">
                list 1 !!!
            </li>
            <li>first list !</li>
            <li>second list !</li>
            <li>third list !</li>
            <li class="start">
                list 5 !!!
            </li>
            <li>other sibling</li>
            <li>other sibling</li>
        </ul>
    </div>
</body>
 
</html>

                    

Output: In the above code, all the previous elements (or siblings) between “list 5” and “list 1” get highlighted with green color.

Example 2: Here is another example of the above-explained method.

html

<!DOCTYPE html>
<html>
 
<head>
    <style>
        .sib * {
            display: block;
            border: 2px solid lightgrey;
            color: black;
            padding: 5px;
            margin: 15px;
        }
    </style>
    <script src=
 
    </script>
    <script>
        $(document).ready(function () {
            $("li.start").prevUntil("li.stop").css({
                "color": "black",
                "border": "2px solid green"
            });
        });
    </script>
</head>
 
<body>
    <div style="width:400px;" class="sib">
        <ul>
            This is parent !!!
            <li class="stop">
                list 1 !!!
            </li>
            <li>first list !</li>
            <li>second list !</li>
            <li>third list !</li>
            <li>fourth list !</li>
            <li>fifth list !</li>
            <li class="start">
                list 7 !!!
            </li>
        </ul>
    </div>
</body>
 
</html>

                    

Output: In the above code, all the previous elements (or siblings) between “list 7” and “list 1” get highlighted with green color.



Last Updated : 11 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads