Home >>jQuery Tutorial >jQuery :only-of-type Selector

jQuery :only-of-type Selector

jQuery :only-of-type Selector

jQuery :only-of-type selector in jQuery is used to select each element of its parent which is the only child of its type.

Syntax:
$(":only-of-type")
Here is an Example of jQuery :only-of-type 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-of-type").css("background-color", "orange");
});
</script>
</head>
<body>
<div style="border:1px solid;">
<p>The first paragraph.</p>
<p>The last paragraph.</p>
</div><br>
<div style="border:1px solid;">
<p>The only paragraph.</p>
</div><br>
<div style="border:1px solid;">
<span>This is a span element.</span>
<p>The only paragraph.</p>
</div><br>
</body>
</html>

Output:

The first paragraph.

The last paragraph.


The only paragraph.


This is a span element.

The only paragraph.



No Sidebar ads