Home >>jQuery Tutorial >jQuery parentsUntil() Method

jQuery parentsUntil() Method

jQuery parentsUntil() Method

jQuery parentsUntil() method in jQuery is used to returns all ancestor (parent, grandparent, great-grandparent, and so on) elements between the selector and stop.

Syntax:
$(selector).parentsUntil(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 ancestors between selector and stop
Here is an Example of jQuery parentsUntil() Method:

<html>
<head>
<style>
.ancestors * { 
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(){
$("span").parentsUntil("div").css({"color": "red", "border": "2px solid #3e88f0"});
});
</script>
</head>
<body>
<div class="ancestors">
<div style="width:500px;">div (great-grandparent)
<ul>ul (grandparent)  
<li>li (direct parent)
<span>span</span>
</li>
</ul>   
</div>
</div>
</body>
</html>

Output:
div (great-grandparent)
    ul (grandparent)
  • li (direct parent) span