Home >>jQuery Tutorial >jQuery children() Method

jQuery children() Method

jQuery children() Method

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

Syntax:
$(selector).children(filter)

Parameter Values

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

<html>
<head>
<style>
.descendants * { 
display: block;
border: 2px solid dodgerblue;
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(){
$("ul").children().css({"color": "red", "border": "2px solid #df65f7"});
});
</script>
</head>
<body class="descendants">
<div style="width:500px;">div (grandparent)
<ul>ul (direct parent)  
<li>li (child)
<span>span (grandchild)</span>
</li>
</ul>   
</div>
</body>
</html>

Output:
div (grandparent)
    ul (direct parent)
  • li (child) span (grandchild)