My objective is to create a mouseover effect on a box that changes its color randomly. Currently, I have managed to achieve this by toggling one class (e.g.
$("#evtTarget").toggleClass(highlighted-2);
). However, I am now attempting to select a random class from a pool of 5 different highlight classes. This means that every time the box is moused over, it will switch to a new random color. Here is my current code:
jQuery
$(function() {
$("#evtTarget").on("mouseover mouseleave", highlight);
});
function highlight(evt) {
var number = Math.floor((Math.random() * 5) + 1);
var colors = "'highlighted'" + "-" + number;
$("#evtTarget").toggleClass(colors);
}
CSS
.highlighted-1 {
background-color: Blue;
}
.highlighted-2 {
background-color: Purple;
}
.highlighted-3 {
background-color: Green;
}
.highlighted-4 {
background-color: Pink;
}
.highlighted-5 {
background-color: Red;
}
.box {
border: solid 1px black;
height: 300px;
width: 300px;
background-color: gray;
}
HTML
<div id="evtTarget" class="box"><p>Random Highlight</p></div>
I appreciate your patience with me as I am still learning.
Thank you for any assistance provided!