[thelist] JavaScript - searching for link and comparing

Pringle, Ron RPringle at aurora-il.org
Tue Dec 21 16:20:22 CST 2004


Hi all-

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.

I've googled around looking at examples of getElementByID, but don't see
examples that match what I'm trying to do.

What I have so far:


Javascript:

var fullLink=document.location.href;
var firstSplit=fullLink.split(".org/");
var relativeLink=(".."+firstSplit[1]);


The nav menu is structured like this:

<ul id="nav">
<li><a href="../aldermen/index.htm">Aldermans Office</a></li>
<li><a href="../aldermen/index.htm">Home Page</a></li>
<li><a href="../aldermen/block_parties.htm">Block Parties</a></li>
</ul>

So I want to search through the element with ID "nav" for an href that
matches the var "relativeLink" and if a match is found, then I want to
change the class of the enclosing <li> tag to class="current".

Suggestions or links to relevant URLs would be appreciated.

Regards,
Ron


More information about the thelist mailing list