Skip to content

Instantly share code, notes, and snippets.

@tw2113
Last active January 13, 2024 02:10
Show Gist options
  • Save tw2113/1804843 to your computer and use it in GitHub Desktop.
Save tw2113/1804843 to your computer and use it in GitHub Desktop.
Random color on hover
color_classes = ['green','purple','violet','teal','pink'];
(function() {
const initLinks = function() {
const div = document.querySelector('#mydiv');
console.log(div)
div.addEventListener('mouseover',function(){
color_classes.forEach(color =>{
div.classList.remove(color);
});
div.classList.add(randomFrom(color_classes));
});
};
const randomFrom = function(array) {
return array[Math.floor(Math.random() * array.length)];
}
initLinks();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment