Skip to content Skip to sidebar Skip to footer

Pure Javascript Replacement For :hover

My goal, essentially, is to have the CSS :hover replaced by JavaScript. The reason is that I need a loop to be executed on a variable number of divs that will be nested inside the

Solution 1:

Basically you can select elements having a particular class using getElementsByClassName.

Here is a working demo.

var elements = document.getElementsByClassName('childbox');
for(var i=0; i<elements.length; i++) { 
  elements[i].onmouseleave = function(){
    this.style.backgroundColor = "blue";
};
}

Solution 2:

So use instead of getElementById this: http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

And provide classess for your divs, where you want have that event.

Post a Comment for "Pure Javascript Replacement For :hover"