A loop that goes over all the divs in a given container incorrectly assigns classes to the onmouseout event
for (var i=0; i<divs.length; i++){ var cssClass = 'box n'+(i+1); //создаем класс 'box n1' divs[i].className = cssClass; divs[i].onmouseover = function(){ this.className= cssClass+' hover' } divs[i].onmouseout = function(){ this.className= cssClass } //console.log(cssClass); Показывает что в каждой итерации класс правильный }
The mistake is that when an event occurs, an element is not hung on its own class, but on the class of the last element, in this case 'box n42', instead of its own. Why does this happen, because the console shows that in each iteration, when the event is hung the class is correct?