Last child (:last-child)

Description and Syntax

$(':last-child')

selects all elements that are the last child of their parent element.

Examples

SelectorSelects
$('li:last-child')all <li> elements that are the last child of their parent element
$(.myclass:last-child')all elements with the class myclass that are the last child of their parent element

<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!-- w w w.j a  va2  s  .  c o  m-->
                $("div span:last-child")
                .css("text-decoration", "underline")
                .hover(function () {
                      $(this).addClass("red");
                    }, function () {
                      $(this).removeClass("red");
                    });
        });
    </script>
    <style>
      span { color:#008; }
      span.red { color:red; font-weight: bolder; }
    </style>
  </head>
  <body>
    <body>
      <div>
          <span>A,</span>
          <span>B,</span>
          <span>C</span>
      </div>
      <div>
          <span>D,</span>
          <span>E,</span>
          <span>F</span>
      </div>
    </body>
</html>

Click to view the demo

The code above generates the following result.

Last child (:last-child)