Got some code here that isn’t working:
$('#sidebar ul li:last').each(function(){ $(this).addClass('last'); });
Basically I have 3 lists and want to add a class (last) to each item appearing last in each unordered list.
<ul> <li>Item 1</li> <li>Item 2</li> <li class='last'>Item 3</li> </ul>
Hope that makes sense, Cheers!
Easy mistake to make. Try this:
Basically :last doesn’t quite do what you think it does. It only matches the very last in the document, not the last in each list. That’s what :last-child is for.