-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (40 loc) · 1.41 KB
/
index.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
const containerDiv = document.getElementById('container');
const sizePromptBtn = document.getElementById('size-prompt');
const clearGridBtn = document.getElementById('clear-grid');
//initial grid size
createGrid(16);
// prompt user for grid size
sizePromptBtn.addEventListener('click', function () {
let size = parseInt(prompt("Enter the size of the grid you want"));
// size = parseInt(size);
console.log(size);
containerDiv.innerHTML = "";
if (size > 100 || isNaN(size) || size <= 0) {
alert("Please enter a valid number")
}
createGrid(size)
})
function createGrid(size) {
document.documentElement.style.setProperty('--size', size);
for (let i = 1; i <= size; i++) {
const rowDiv = document.createElement('div');
rowDiv.className = 'row-div';
containerDiv.append(rowDiv);
// rowDiv.textContent = i;
for (let j = 1; j <= size; j++) {
const boxDiv = document.createElement('div');
boxDiv.className = 'box-div';
rowDiv.append(boxDiv);
// event listner
boxDiv.addEventListener('mouseover', function () {
boxDiv.style.backgroundColor = 'green';
})
}
}
}
clearGridBtn.addEventListener('click', function () {
const boxDivs = document.querySelectorAll('.box-div');
boxDivs.forEach(boxDiv => {
boxDiv.style.backgroundColor = 'black';
});
});