homepage Welcome to WebmasterWorld Guest from 204.236.254.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Add class to last list item
greencode




msg:4274683
 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?

 

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved