I need help with randomly coloring 3 div elements using JavaScript-controlled CSS.
<div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
<button>Enter</button>
Here is the code I have so far:
var randomColor = Math.ceil(Math.random() * 3);
var color = "";
//Accessing the divs
var box1 = document.querySelector("#box1");
var box2 = document.querySelector("#box2");
var box3 = document.querySelector("#box3");
//Event
var button = document.querySelector("button");
button.addEventListener("click", randomize, false);
button.style.cursor = "pointer";
function render(){
box1.style.background = color;
box2.style.background = color;
box3.style.background = color;
}
function randomize(randomColor){
switch(randomColor){
case 1:
color = "red";
break;
case 2:
color = "blue";
break;
case 3:
color = "green";
break;
}
render();
}
However, all three divs end up with the same color. Can anyone suggest a solution? I would like to stick with pure JavaScript and CSS without resorting to jQuery if possible. Thank you for your help!