This method is used to return the first element of the selected elements.
Example :
HTML Code :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="border: 1px solid black;">
<p>This is the first paragraph in a div.</p>
<p>This is the last paragraph in a div.</p>
</div><br>
<div style="border: 1px solid black;">
<p>This is the first paragraph in another div.</p>
<p>This is the last paragraph in another div.</p>
</div>
</body>
</html>
jQuery Code :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p").first().css("background-color", "yellow");
});
</script>
Output :
This is the first paragraph in a div.// first method change the color of this p tag i.e first ( ) method
This is the last paragraph in a div.
This is the first paragraph in another div.
This is the last paragraph in another div.
0 Comment(s)