-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (41 loc) · 1.5 KB
/
script.js
File metadata and controls
45 lines (41 loc) · 1.5 KB
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
const container = document.getElementById("grid");
const resetBtn = document.getElementById("reset");
const blackBtn = document.getElementById("black");
const redBtn = document.getElementById("red");
const blueBtn = document.getElementById("blue");
const eraserBtn = document.getElementById("eraser");
function createGrid(gridSize){
for (i = 0; i < gridSize ** 2; i++) {
const cell = document.createElement("div");
cell.classList.add("cells");
cell.style.border = "1px solid black";
container.appendChild(cell);
container.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
container.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
}
let color = "black";
const gridCells = document.querySelectorAll('.cells');
gridCells.forEach(cell => cell.addEventListener("mouseover", function(){
if(blackBtn.checked == true){
cell.style.backgroundColor = "black";
}else if (blueBtn.checked == true){
cell.style.backgroundColor = "blue";
}else if (redBtn.checked == true){
cell.style.backgroundColor = "red";
}else if (eraserBtn.checked == true){
cell.style.backgroundColor = "white";
}
}))
}
function resetGrid(){
const userGrid = prompt("How many Cells do you want? (1-100)");
if (userGrid >= 1 && userGrid <= 100){
const gridCells = document.querySelectorAll(".cells");
gridCells.forEach(gridCell => gridCell.remove());
createGrid(userGrid);
}
else {
alert("Choose a number between 1-100!");
}
}
window.onload = createGrid(16);