<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div class="a" style='font-size:50'>a</div>
<div class="b" style='font-size:50'>b</div>
<div class="c" style='font-size:50'>c</div>
<script>
var classes = ['a', 'b', 'c'];
for (var i = 0; i < classes.length; i++) {
$("."+classes[i]).mouseover(function() {
console.log(i);
console.log(classes[i]);
});
}
</script>
</body>
</html>
This is the simplest reproduction of the issue I could create. When mouseovering the elements, there are two things wrong:
log(i)
always logs '3', which doesn't make sense because the loop ends at 2.classes[i]
returns Undefined, even though I just defined it at the top of the script.