jQuery :first Selector (original) (raw)
Last Updated : 17 Nov, 2022
The jQuery :first Selector is used to select the first element of the specified type.
Syntax:
$(":first")
Example 1: In this example, we will select the first
element by using jQuery :first Selector.
HTML `
<script>
$(document).ready(function () {
$("p:first").css(
"background-color", "green");
});
</script>
Geeks
Geeks for Geeks
jQuery
First Selector
`
Output:
Example 2: In this example, we will select the first
element of the first
by using jQuery :first Selector.
HTML `
<script>
$(document).ready(function () {
$("p:first").css(
"background-color", "green");
});
</script>
Geeks
Geeks for Geeks
jQuery
Geeks for Geeks
jQuery
First Selector
jQuery:First Selector
`
Output:
Note: Above code will select only the first
element of first div. To select the first
element of second div use :first-child selector.