This is something that has me stumped, I have the following table:
I want to add an onmouseover/onmouseout to each "A" tag onload and can do it if I access each one individually:
But I want to put that into a loop, which doesn't work
Here is the full code of my attempt:
--------
GOOGLE is a great resource to find answers to questions like "how do i..."
If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
HTML:
<table border="1" id="tblNav">
<tr>
<td width="130">
<a id="nav_0" href="/home/" >HOME</a>
</td>
<td width="130">
<div><a id="nav_1" href="/about/">ABOUT</a>
</td>
<td width="130">
<a id="nav_2" href="/contact/">CONTACT</a>
</td>
</tr>
</table>
I want to add an onmouseover/onmouseout to each "A" tag onload and can do it if I access each one individually:
Code:
window.onload = function () {
$("nav_0").onmouseover = function () { toggleNav(0); }
$("nav_0").onmouseout = function () { toggleNav(0); }
$("nav_1").onmouseover = function () { toggleNav(1); }
$("nav_1").onmouseout = function () { toggleNav(1); }
$("nav_2").onmouseover = function () { toggleNav(2); }
$("nav_2").onmouseout = function () { toggleNav(2); }
}
But I want to put that into a loop, which doesn't work
Code:
window.onload = function () {
var navItems = $("tblNav").getElementsByTagName("a");
for (var i = 0 ; i < navItems.length ; i++) {
navItemId = navItems[i].id;
$(navItemId).onmouseover = function () { toggleNav(i); }
$(navItemId).onmouseout = function () { toggleNav(i); }
}
}
Here is the full code of my attempt:
Code:
<script language="javascript">
function $(strInput) {
return document.getElementById(strInput);
}
window.onload = function () {
var navItems = $("tblNav").getElementsByTagName("a");
for (var i = 0 ; i < navItems.length ; i++) {
navItemId = navItems[i].id;
$(navItemId).onmouseover = function () { toggleNav(i); }
$(navItemId).onmouseout = function () { toggleNav(i); }
}
}
var strOn = "GO HOME,WHO WE ARE,TALK TO US";
var strOff = "HOME,ABOUT,CONTACT";
var raOn = strOn.split(",");
var raOff = strOff.split(",");
function toggleNav(intItem) {
$("nav_" + intItem).innerHTML
= ($("nav_" + intItem).innerHTML == raOn[intItem])
? raOff[intItem]
: raOn[intItem];
}
</script>
<table border="1" id="tblNav">
<tr>
<td width="130">
<a id="nav_0" href="/home/" >HOME</a>
</td>
<td width="130">
<div><a id="nav_1" href="/about/">ABOUT</a>
</td>
<td width="130">
<a id="nav_2" href="/contact/">CONTACT</a>
</td>
</tr>
</table>
--------
GOOGLE is a great resource to find answers to questions like "how do i..."
If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc