Hello Readers,
parent() is the jquery method which is used to return the parent element of the given selector or the current element in jquery. We use parent() method without parameter as default but if we pass any parameter inside the parent() method then this method filter the element as per filter parameter.
Syntax :
$(selector).parent(filter)
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").parent().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 parent() method and this method apply on <span> tag and change the color and border of the <span> parent.
0 Comment(s)