View Full Version : Check whether list item is last in list

07-27-2006, 01:08 AM

I'm working on a script that modifies an unordered list. In de script I loop through an array which contains al list items:

var menuItems = tree.getElementsByTagName('LI');
for(var id=0; id<menuItems.length; id++) {
// modify list items
// e.g.:
menuItems[id].style.backgroundImage = 'url(\'bg_image.jpg\')';

Now I'm looking for a way to check whether the current list item is the last element of the tree, in order to assign a different background image to the last list item.

How can I do this?

Thanx! -Michiel

07-27-2006, 02:03 AM
within a loop.

if(id==menuitems.length-1) { }
or loop

for(var id=0; id<menuItems.length-1; id++) {}
then id will be last index