Home >>jQuery Tutorial >jQuery parents() Method

jQuery parents() Method

jQuery parents() Method

jQuery parents() method in jQuery is used to returns all ancestor (An ancestor is a parent, grandparent, great-grandparent, and so on) elements of the selected element.


Parameter Values

Parameter Description
filter It is an Optional parameter and is used to specifies a selector expression to narrow down the search for ancestors
Here is an Example of jQuery parents() Method:

.ancestors * { 
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
$("span").parents().css({"color": "#22a5bf", "border": "2px solid #22a5bf"});
<div class="ancestors">
<div style="width:500px;">div (great-grandparent)
<ul>ul (grandparent)  
<li>li (direct parent)

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