-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
57 lines (46 loc) · 1.58 KB
/
main.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
const container = document.querySelector(".container");
const inputColor = document.querySelector(".pickcolor-btn");
const gridRange = document.querySelector("#grid-selection");
const gridRangeText = document.querySelector("#grid-selection-txt");
const resetBtn = document.querySelector(".reset-btn");
let numberOfSquares = gridRange.value;
let squareColor;
gridRangeText.innerText = `${gridRange.value} x ${gridRange.value}`;
const generateGrid = () => {
const containerWidth = 550;
const squarePerimeter = containerWidth / numberOfSquares;
for (let i = 1; i <= numberOfSquares; i++) {
const row = document.createElement("div");
row.classList.add("row");
container.appendChild(row);
for (let j = 1; j <= numberOfSquares; j++) {
const item = document.createElement("div");
item.classList.add("item");
item.style.width = `${squarePerimeter}px`;
item.style.height = `${squarePerimeter}px`;
row.appendChild(item);
item.addEventListener("mouseover", () => {
item.style.backgroundColor = squareColor;
});
}
}
};
generateGrid();
const deleteCurrentGrid = () => {
while (container.firstChild) {
container.removeChild(container.firstChild);
}
};
const regenerateGrid = () => {
deleteCurrentGrid();
numberOfSquares = gridRange.value;
generateGrid();
};
gridRange.addEventListener("change", () => {
gridRangeText.innerText = `${gridRange.value} x ${gridRange.value}`;
regenerateGrid();
});
inputColor.addEventListener("change", () => {
squareColor = inputColor.value;
});
resetBtn.addEventListener("click", regenerateGrid);