Home >>jQuery Tutorial >jQuery :only-child Selector

jQuery :only-child Selector

jQuery :only-child Selector

jQuery :only-child selector in jQuery is used to pick each item which is its parent's only child.

Syntax:
$(":only-child")
Here is an Example of jQuery :only-child Selector:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p:only-child").css("background-color", "orange");
});
</script>
</head>
<body>
<div style="border:1px solid;">
<p>The first child.</p>
<p>The last child.</p>
</div><br>
<div style="border:1px solid;">
<p>The only child.</p>
</div><br>
<div style="border:1px solid;">
<span>The first child.</span>
<p>The last child.</p>
</div><br>
</body>
</html>

Output:

The first child.

The last child.


The only child.


The first child.

The last child.



No Sidebar ads