jQuery Selector :last

Introduction

The :last selector selects the last element.

This selector can only select one single element.

The :last-child selector can select elements for each parent.

$(":last")

Select the last <p> 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(){
  $("p:last").css("background-color", "yellow");
});/* ww w.  ja  v a 2  s .  c  o  m*/
</script>
</head>
<body>

<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the last paragraph.</p>

</body>
</html>



PreviousNext

Related