I've created a css menu, and I want to make each one of them to change their color on mouseover event (I'm just learning javascript, and specially the for loop, I know can achieve this with CSS3).
So, It's not working, I get this error on the DOM console: Uncaught ReferenceError: linkIdOn is not defined (line 44)
This is my CSS:
<body>
<div id="menuPrincipal">
<div id="link1" class="link"><a href="">Link 1</a></div>
<div id="link2" class="link"><a href="">Link 2</a></div>
<div id="link3" class="link"><a href="">Link 3</a></div>
<div id="link4" class="link"><a href="">Link 4</a></div>
</div>
This is my javascript:
function cambioColorOnMouseover(){
for (linkId=1; linkId<5; linkId++){
var linkIdOn='link'+linkId;
document.getElementById(linkIdOn).style.backgroundColor="#eee";
console.log(linkIdOn);
}
}
function cambioColorOnLeave(){
for (linkId=1; linkId<5; linkId++){
var linkIdOff='link'+linkId;
document.getElementById(linkIdOff).style.backgroundColor="#ccc";
console.log(linkIdOff);
}
}
document.getElementById('link'+linkIdOn).onmouseover=cambioColorOnMouseover; <-- line 44
document.getElementById('link'+linkIdOff).onmouseout=cambioColorOnLeave;