HTML
?
!
\"
&l
var charElems = document.getElementsByClassName('char');
for (var i = 0; i < charElems.length; i++) {
//close...
//charElems[i].addEventListener('mouseover',function() {
// (function(j) {mouseoverCheck(j);}(i));
//});
//like this
(function(el, x) {
el.addEventListener("mouseover", function() {
mouseoverCheck(x);
});
})(charElems[i], i)
}
it doesn't work because
charElems[i].addEventListener('mouseover',function() {
(function(j) {mouseoverCheck(j);}(i));
});
addEventListener()
is just assigning a handler and by the time that handler is called i
will be 6.
you should return a handler from an IIFE
var charElems = document.getElementsByClassName('char');
for (var i=0; i < charElems.length; i++) {
charElems[i].addEventListener('mouseover', (function(temp) {
return function(){
var j = temp;
//mouseoverCheck(j);
console.log(temp);
}
}(i)));
}
Here is a fiddle: https://jsfiddle.net/qshnfv3q/