jQuery last()

Introduction

The last() method returns the last element of the selected elements.

$(selector).last()

Select the last <p> element inside the last <div> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("div p").last().css("background-color", "yellow");
});//from www  .  j a  v  a2  s .co m
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<div style="border:1px solid black">
  <p>This is a paragraph in a div.</p>
  <p>This is a paragraph in a div.</p>
</div><br>

<div style="border:1px solid black">
  <p>This is a paragraph in another div.</p>
  <p>This is a paragraph in another div.</p>
</div>

<p>This is also a paragraph.</p>

</body>
</html>



PreviousNext

Related