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>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
script>
<script>
$(document).ready(function () {
$("p:first").css(
"background-color", "green");
});
script>
head>
<body>
<h1>
<center>Geekscenter>
h1>
<p>Geeks for Geeksp>
<p>jQueryp>
<p>First Selectorp>
body>
html>
Output:
Example 2: In this example, we will select the first
element of the first
by using jQuery :first Selector.
HTML
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
script>
<script>
$(document).ready(function () {
$("p:first").css(
"background-color", "green");
});
script>
head>
<body>
<h1>
<center>Geekscenter>
h1>
<div style="border:1px solid;">
<p>Geeks for Geeksp>
<p>jQueryp>
div>
<br>
<div style="border:1px solid;">
<p>Geeks for Geeksp>
<p>jQueryp>
<p>First Selectorp>
div>
<p>jQuery:First Selectorp>
body>
html>
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.Â