Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- Add class to last list item


greencode - 4:44 pm on Mar 1, 2011 (gmt 0)


I'm struggling a bit with this one. I have the following HTML:


<ul id="side-nav">
<li class="trigger"><a href="#">Main heading 1</a></li>
<li class="toggle-container">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
</li>
<li class="trigger"><a href="#">Main heading 2</a></li>
<li class="toggle-container">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
</li>
</ul>


and then I'm trying to dynamically add the class of "last" to the last list items using jQuery i.e. the ones that are labelled Item 3 in both lists.

$(document).ready(function() {
$("ul#side-nav ul li:last").addClass("last");
});


Doesn't seem to be happening though - any ideas?


Thread source:: http://www.webmasterworld.com/javascript/4274681.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com