Created
March 11, 2018 14:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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