Fotiman

msg:4274721 | 5:04 pm on Mar 1, 2011 (gmt 0) |
:last [api.jquery.com] selects only a single element, the last one matching the selector. What you want is the :last-child [api.jquery.com] selector, which can match more than one. So: $(document).ready(function() { $("ul#side-nav ul li:last-child").addClass("last"); });
|
|
|
birdbrain

msg:4274722 | 5:04 pm on Mar 1, 2011 (gmt 0) |
Hi there greencode, I know absolutely nothing about "jQuery" other than it seems like taking a sledgehammer to crack an egg in many instances. ;) Here is a basic javascript solution to your problem... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="language" content="english"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>last li in list</title> <style type="text/css"> .last,.last a { color:#f00; } </style> <script type="text/javascript"> function init(){ var lis=document.getElementById('side-nav').getElementsByTagName('li'); lis[lis.length-1].className='last'; } window.addEventListener? window.addEventListener('load',init,false): window.attachEvent('onload',init); </script> </head> <body> <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> </body> </html>
|
| birdbrain
|
Fotiman

msg:4274735 | 5:08 pm on Mar 1, 2011 (gmt 0) |
birdbrain, your approach seems to only attach the class to the last <li> element descendant of the element with id 'side-nav', while greencode is looking to attach it to the last <li> element of each <ul> element within 'side-nav'.
|
greencode

msg:4274742 | 5:22 pm on Mar 1, 2011 (gmt 0) |
Thanks for both your help with this - Fotiman it was indeed the "last-child" that I needed to add and not "last". It's been a long day!
|
|