Hello Readers,
parentsUntil() is the jquery method which is used to return all the ancestors between the selector and stop.Here, ancestor element include parent, grandparent, great-grandparent, great-great grandparent and so on. This method also used without parameter as default but if we pass the parameter (stop) then it will search until the stop parameter and if we also pass (filter) parameter then it returns the result by filter parameter in jquery.
Syntax :
$(selector).parentsUntil(stop,filter)
stop : stop is the optional parameter in which a selector expression or element indicating where to stop the search for matching ancestor elements.
filter : filter is also a optional parameter which specifies a selector expression to search down the ancestors between selector and stop paremeters.
Code Example :
<html>
<head>
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("span").parentsUntil("div").css({"color": "red", "border": "2px solid red"});
});
</script>
</head>
<body class="ancestors"> body (great-great-grandparent)
<div style="width:500px;">div (great-grandparent)
<ul>ul (grandparent)
<li>li (direct parent)
<span>span</span>
</li>
</ul>
</div>
</body>
</html>
In the above code we use the parentsUntil() method and return all ancestor elements between <span> and the <div> element in jquery.
0 Comment(s)