Home >>jQuery Tutorial >jQuery prevUntil() Method

jQuery prevUntil() Method

jQuery prevUntil() Method

jQuery prevUntil() method in jQuery is used to returns all previous sibling elements (share the same parent) between the selector and stop.

Syntax:
$(selector).prevUntil(stop,filter)

Parameter Values

Parameter Description
stop It is an optional parameter and is used to a selector expression, element or jQuery object indicating
filter It is an optional parameter and is used to specifies a selector expression to narrow down the search for sibling elements between the selector and stop and also separate each expression with a comma.
Here is an Example of jQuery prevUntil() Method:

<html>
<head>
<style>
.siblings * { 
display: block;
border: 2px solid #cfe2fc;
color: #cfe2fc;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("li.start").prevUntil("li.stop").css({"color": "red", "border": "2px solid #3e88f0"});
});
</script>
</head>
<body>
<div style="width:500px;" class="siblings">
<ul>ul (parent)  
<li class="stop">li (sibling with class name "stop")</li>
<li>li (the previous sibling of li with class name "start")</li>
<li>li (the previous sibling of li with class name "start")</li>
<li>li (the previous sibling of li with class name "start")</li>
<li class="start">li (sibling with class name "start")</li>
<li>li (sibling)</li>
<li>li (sibling)</li>
</ul>   
</div>
</body>
</html>

Output:
    ul (parent)
  • li (sibling with class name "stop")
  • li (the previous sibling of li with class name "start")
  • li (the previous sibling of li with class name "start")
  • li (the previous sibling of li with class name "start")
  • li (sibling with class name "start")
  • li (sibling)
  • li (sibling)

No Sidebar ads