jQuery Last child selector

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(){<!-- www .jav a 2  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





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities