:last-child Selector - Javascript jQuery Selector

Javascript examples for jQuery Selector:last-child

Description

The :last-child selector selects elements who are the last child of their parent.

The following code shows how to select every <p> element that is the last child of div parent:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("div p:last-child").css("background-color", "red");
});// w  w  w  .  ja v  a2  s .c o  m
</script>
</head>
<body>

<p>The first paragraph in body.</p>

<div style="border:1px solid;">
  <p>The first paragraph in div.</p>
  <p>The last paragraph in div.</p>
</div><br>

<div style="border:1px solid;">
  <p>The first paragraph in another div.</p>
  <p>E</p>
</div>

<p>A</p>

</body>
</html>

Related Tutorials