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

jQuery :first-of-type Selector

jQuery :first-of-type Selector

jQuery :first-of-type of selector in jQuery is used to pick all items that are their parent's first child, of a specific type.

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

Output:

The first paragraph in body.

The first paragraph in div.

The second paragraph in div.


This is a span element.

The first paragraph in div.

The second paragraph in div.

The last paragraph in body.


No Sidebar ads