Home >>jQuery Tutorial >jQuery first() Method

jQuery first() Method

jQuery first() Method

jQuery first() method in jQuery is used to returns the first element of the selected elements.

Syntax:
$(selector).first()
Here is an Example of jQuery first() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".box .txt").first().css("background-color", "#df65f7");
});
</script>
</head>
<body>
<h1>Welcome to Phptpoint</h1>
<div class="box" style="border:1px solid black">
<p class="txt">This is First paragraph in a div.</p>
<p class="txt">This is Second paragraph in a div.</p>
</div><br>
<div class="box" style="border:1px solid black">
<p class="txt">This is Third paragraph in another div.</p>
<p class="txt">This is Fourth paragraph in another div.</p>
</div>
</body>
</html>

Output:

Welcome to Phptpoint

This is First paragraph in a div.

This is Second paragraph in a div.


This is Third paragraph in another div.

This is Fourth paragraph in another div.


No Sidebar ads