I have a couple of jQuery function with a similar syntax.
$("#item-1").hover(function(){
$(".item-1").animate({opacity:1},"slow");
},function(){
$(".item-1").animate({opacity:0},"slow");
});
$("#item-2").hover(function(){
$(".item-2").animate({opacity:1},"slow");
},function(){
$(".item-2").animate({opacity:0},"slow");
});
$("#item-3").hover(function(){
$(".item-3").animate({opacity:1},"slow");
},function(){
$(".item-3").animate({opacity:0},"slow");
});
My question is how to shorten my code with the help of a loop. I tried the following but that didn’t work:
for (i = 1; i <= 3; ++i) {
$("#item-" + i).hover(function(){
$(".item-" + i).animate({opacity:1},"slow");
},function(){
$(".item-" + i).animate({opacity:0},"slow");
});
}