:last-child Selector - Javascript jQuery Selector

Javascript examples for jQuery Selector:last-child


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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    $("div p:last-child").css("background-color", "red");
});// w  w  w  .  ja v  a2  s .c o  m

<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 style="border:1px solid;">
  <p>The first paragraph in another div.</p>



Related Tutorials