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


birdbrain - 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


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