I’m trying to write a function to set display of containers when hovering over another element.
I have an unordered list with 5 list items each of which, when hovered, should trigger the display of a corresponding div.
I can get each of the individual portions of this to function;i.e. the “hover function” on it’s own, and the “for loop” on it’s own. But when combined, my counting variable (when placed within the hover function, has a fixed value of “6” though the loop iterates the expected number of times “5”.
How do I pass the variable so that it will increment properly?
$(document).ready(function() {
myDivs = '5';
for(z=1;z<=myDivs;z++){
alert('#div_'+ z); //Test for value: here z increments
$('li').hover(
function() {
$('#div_'+ z).addClass("hover");
alert('#div_'+ z); //Test for value: here z equal to 6 all the time ???
},
function() {
$('#div_'+ z).removeClass("hover");
}
);
}
});Any advice for the programmaticlly challenged designer would be much appreciated.
Thanks.