[thelist] JavaScript - searching for link and comparing

Christian Heilmann lists at onlinetools.org
Wed Dec 22 05:08:37 CST 2004

Peter-Paul Koch wrote:

>>I'm a neophyte when it comes to Javascript. I'm trying to write some code
>>that will search through my nav for a link matching the current page, and
>>then write a css class to the containing <li> to style it as the current
>>page the user is on.
>var x = document.getElementById('nav').getElementsByTagName('a');
>for (var i=0;i<x.length;i++)
>  if (x[i].href == location.href)
>    x[i].className = 'youarehere';
That adds the class to the Link though, not the LI, as shown in my 
example you just need to loop over the LIs ammend the comparison to read 
the first link inside the current LI.

Otherwise, the same issues apply. Navigation generation and highlighting 
is an SSI thing, period.

More information about the thelist mailing list