Skip to content

Instantly share code, notes, and snippets.

@WuerfelDev
Created March 11, 2018 14:42
Show Gist options
  • Save WuerfelDev/167008d96fd6db5ae6ad8b92c26e9810 to your computer and use it in GitHub Desktop.
Save WuerfelDev/167008d96fd6db5ae6ad8b92c26e9810 to your computer and use it in GitHub Desktop.
change background color for all elements with the same class by hovering one
function hoverByClass(classname,colorover,colorout="transparent"){
var elms=document.getElementsByClassName(classname);
for(var i=0;i<elms.length;i++){
elms[i].onmouseover = function(){
for(var k=0;k<elms.length;k++){
elms[k].style.backgroundColor=colorover;
}
};
elms[i].onmouseout = function(){
for(var k=0;k<elms.length;k++){
elms[k].style.backgroundColor=colorout;
}
};
}
}
// init / set colors for classes
hoverByClass("classname1","yellow");
hoverByClass("classname2","#c5c5c5");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment