I'm working with this function, but it doesn't work correctly: only the last element works.
Here is my JavaScript code:
function hover_action() {
var array = ["elem1","elem2","elem3","elem4"];
$(array).each(function( index, value ) {
value1 = "#box-" + value + " img";
value2 = "div#name-" + value;
$(value1).hover(function(){
$(value2).fadeIn(300);
}, function(){
$(value2).fadeOut(300);
});
});
}
Her is my HTML code:
<div>
<div id="box-elem1"><img src="path1" /></div>
<div id="name-elem1"><span>NAME 1</span><div>
</div>
<div>
<div id="box-elem2"><img src="path2" /></div>
<div id="name-elem2"><span>NAME 2</span><div>
</div>
<div>
<div id="box-elem3"><img src="path3" /></div>
<div id="name-elem3"><span>NAME 3</span><div>
</div>
.
.
.
Thanks for your help.
value2(andvalue1btw but doesn't really matter here) as global, that's why. Usevarstatement:var value2 = "div#name-" + value;