Home >>jQuery Tutorial >jQuery last() Method

jQuery last() Method

jQuery last() Method

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

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

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

Output:

Welcome to Phptpoint

This is First Paragraph

This is Second Paragraph


This is Third Paragraph

This is Fourth Paragraph