jQuery | prevUntil() with Examples
The prevUntil() is an inbuilt method in jQuery which 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 for 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”.
Code #1:
<html> <head> <style> .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() { $("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> |
In the above code, all the previous elements (or siblings) between “list 5” and “list 1” get highlighted with green color.
Output:

Code #2:
<html> <head> <style> .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() { $("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> |
In the above code, all the previous elements (or siblings) between “list 7” and “list 1” get highlighted with green color.
Output:

Recommended Posts:
- jQuery | has() with Examples
- jQuery | first() with Examples
- jQuery | after() with Examples
- jQuery | last() with Examples
- jQuery | on() with Examples
- jQuery | one() with Examples
- jQuery | eq() with Examples
- jQuery | val() with Examples
- jQuery | replaceWith() with Examples
- jQuery | submit() with Examples
- jQuery | resize() with Examples
- jQuery | replaceAll() with Examples
- jQuery | removeAttr() with Examples
- jQuery | removeProp() with Examples
- jQuery | scrollLeft() with Examples
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.



