last-child selector
Description: Selects all elements that are the last child of their parent.
jQuery(':last-child')
version added: 1.1.4
While :last matches only a single element, :last-child can match more than one: one for each parent.
Examples
Finds the last span in each matched div and adds some css plus a hover state.
$("div span:last-child")
.css({color:"red", fontSize:"80%"})
.hover(function () {
$(this).addClass("solast");
}, function () {
$(this).removeClass("solast");
});
Full source:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ $("div span:last-child") $("div span:last-child") .css({color:"red", fontSize:"80%"}) .hover(function () { $(this).addClass("solast"); }, function () { $(this).removeClass("solast"); }); }); </script> <style> span.solast { text-decoration:line-through; } </style> </head> <body> <div> <span>John,</span> <span>Karl,</span> <span>Brandon,</span> <span>Sam</span> </div> <div> <span>Glen,</span> <span>Tane,</span> <span>Ralph,</span> <span>David</span> </div> </body> </html>
Was this information helpful?

