allida77
05-08-2003, 09:46 PM
**html
<div id="topmenu">
<span id="mnuItemHome" class="topmenuItem">Home</span>
<span id="mnuItemOrders" class="topmenuItem">Orders</span>
<span id="mnuItemContact" class="topmenuItem">Contact</span>
<span id="mnuItemAdmin" class="topmenuItem">Administration</span>
</div>
**js
var objMnu = document.getElementById("topmenu")
aMnuItems= objMnu.getElementsByTagName("span");
//this will not work
for(i=0;i<aMnuItems.length;i++){
document.getElementById(aMnuItems[i].id).attachEvent("onmouseover",function over(){document.getElementById(aMnuItems[i].id).style.backgroundColor="red"});
document.getElementById(aMnuItems[i].id).attachEvent("onmouseout",function out(){document.getElementById(aMnuItems[i].id).style.backgroundColor="white";});
}
I do not understand why I can not loop through and attach these events. I tried just hard coding the id of the elements and it will work but when I try to loop over them it doesnt work. This may not be the best approach, but I just dont get why its not working.
<div id="topmenu">
<span id="mnuItemHome" class="topmenuItem">Home</span>
<span id="mnuItemOrders" class="topmenuItem">Orders</span>
<span id="mnuItemContact" class="topmenuItem">Contact</span>
<span id="mnuItemAdmin" class="topmenuItem">Administration</span>
</div>
**js
var objMnu = document.getElementById("topmenu")
aMnuItems= objMnu.getElementsByTagName("span");
//this will not work
for(i=0;i<aMnuItems.length;i++){
document.getElementById(aMnuItems[i].id).attachEvent("onmouseover",function over(){document.getElementById(aMnuItems[i].id).style.backgroundColor="red"});
document.getElementById(aMnuItems[i].id).attachEvent("onmouseout",function out(){document.getElementById(aMnuItems[i].id).style.backgroundColor="white";});
}
I do not understand why I can not loop through and attach these events. I tried just hard coding the id of the elements and it will work but when I try to loop over them it doesnt work. This may not be the best approach, but I just dont get why its not working.