dog
03-29-2008, 05:40 PM
Hello!
Here's a simple bit of code.
function selectLink(elementName) {
obj = document.getElementById(elementName);
obj.className = "selected";
}
It adds the class 'selected' to a link in my menu.
I call it when the pages loads, like this:
<body onload="selectLink('aboutLink1')">
It works because I've given the links IDs, like this:
<ul class="greyMenu1">
<li><a href="../about" id="aboutLink1">About</a></li>
<li><a href="../contact" id="contactLink1">Contact</a></li>
<li><a href="../purchase" id="purchaseLink1">Purchase</a></li>
</ul>
The thing is, I've got another menu that I also want to use the javascript on, it looks like this:
<ul class="greyMenu1">
<li><a href="../about" id="aboutLink2">About</a></li>
<li><a href="../contact" id="contactLink2">Contact</a></li>
<li><a href="../purchase" id="purchaseLink2">Purchase</a></li>
</ul>
So, in the onload command I'm calling the javascript once for the first menu and once for the second, like this:
<body onload="selectLink('aboutLink1'),selectLink('aboutLink2')">
What I'd like to do is change the javascript so that it can deal with both the elements at once. Effectively I'd like to reduce my HTML to this:
<body onload="selectLink('aboutLink1','aboutLink2')">
However, I don't know how to change the javascript to have it deal with more than one element in one go. If anyone knows how, I would really appriciate the lesson.
Thanks,
Dog
Here's a simple bit of code.
function selectLink(elementName) {
obj = document.getElementById(elementName);
obj.className = "selected";
}
It adds the class 'selected' to a link in my menu.
I call it when the pages loads, like this:
<body onload="selectLink('aboutLink1')">
It works because I've given the links IDs, like this:
<ul class="greyMenu1">
<li><a href="../about" id="aboutLink1">About</a></li>
<li><a href="../contact" id="contactLink1">Contact</a></li>
<li><a href="../purchase" id="purchaseLink1">Purchase</a></li>
</ul>
The thing is, I've got another menu that I also want to use the javascript on, it looks like this:
<ul class="greyMenu1">
<li><a href="../about" id="aboutLink2">About</a></li>
<li><a href="../contact" id="contactLink2">Contact</a></li>
<li><a href="../purchase" id="purchaseLink2">Purchase</a></li>
</ul>
So, in the onload command I'm calling the javascript once for the first menu and once for the second, like this:
<body onload="selectLink('aboutLink1'),selectLink('aboutLink2')">
What I'd like to do is change the javascript so that it can deal with both the elements at once. Effectively I'd like to reduce my HTML to this:
<body onload="selectLink('aboutLink1','aboutLink2')">
However, I don't know how to change the javascript to have it deal with more than one element in one go. If anyone knows how, I would really appriciate the lesson.
Thanks,
Dog