Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: open

Message Too Old, No Replies

Add class to last list item

     

greencode

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

5+ Year Member



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

5:04 pm on Mar 1, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



: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

5:04 pm on Mar 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

5:08 pm on Mar 1, 2011 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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

5:22 pm on Mar 1, 2011 (gmt 0)

5+ Year Member



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!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month