-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (56 loc) · 2.36 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Creates the grid of divs
function createDivs(gridCount){
for(let i = 0; i < gridCount; i++){
for(let j = 0; j < gridCount; j++){
let div = document.createElement("div");
let size = document.getElementById("container").clientWidth;
div.style.backgroundColor = "white";
div.style.width = `${size/gridCount}px`;
div.style.height = `${size/gridCount}px`;
div.classList.add("div-boi");
document.getElementById("container").appendChild(div);
}
}
}
//Removes all divs (used when changing grid size)
function removeAllDivs(parent){
while(parent.firstChild){
parent.removeChild(parent.firstChild);
}
}
createDivs(16);
const container = document.querySelector('#container');
let allDivs = document.querySelectorAll(".div-boi");
let blackRadioButton = document.querySelector("#black-color");
let rainbowRadioButton = document.querySelector("#rainbow-color");
let newGridButton = document.querySelector("#new-grid");
// Event listener that checks to see if "NEW GRID" button was clicked
newGridButton.addEventListener('click', () => {
removeAllDivs(container);
let gridSize = 0;
while(gridSize > 40 || gridSize < 1 || gridSize === null || typeof(gridSize) != "number" || isNaN(gridSize)){
gridSize = parseInt(prompt("Enter a number for the grid size < 40 \ni.e. '16' would give a 16x16 grid", 16));
}
createDivs(gridSize);
});
// Event listener to see if the black radio button has been selected (and wasn't already selected)
blackRadioButton.addEventListener('change', () => {
let allDivs = document.querySelectorAll(".div-boi");
allDivs.forEach(element => {
element.addEventListener('mouseenter', () => {
element.style.backgroundColor = "black";
});
});
});
// Event listener to see if the rainbow radio button has been selected (and wasn't already selected)
rainbowRadioButton.addEventListener('change', () => {
let allDivs = document.querySelectorAll(".div-boi");
allDivs.forEach(element => {
element.addEventListener('mouseenter', () => {
let red = Math.round(Math.random() * 255);
let green = Math.round(Math.random() * 255);
let blue = Math.round(Math.random() * 255);
element.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
});
});
});