Home >>jQuery Tutorial >jQuery parent() Method

jQuery parent() Method

jQuery parent() Method

jQuery parent() method in jQuery is used to returns the direct parent element of the selected element.

Syntax:

$(selector).parent(filter)

Parameter Values

Parameter Description
filter It is an Optional parameter and used to specifies a selector expression to narrow down the parent search

Here is an Example of jQuery parent() Method:


<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/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("span").parent().css({"color": "red", "border": "2px solid #4abdd4"});
});
</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

No Sidebar ads