This repository was archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (48 loc) · 1.82 KB
/
script.js
File metadata and controls
67 lines (48 loc) · 1.82 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const body = document.querySelector('body');
// add button to change grid size
const btn = document.createElement('button');
btn.setAttribute('class', 'reSize');
btn.textContent = 'change grid size';
body.appendChild(btn);
// make container <div>
const div = document.createElement('div');
div.classList.add('container');
div.setAttribute('class', 'container');
body.appendChild(div);
let gridSize = 16; // initial grid size
// make multiple divs (16 * 16) using 2 loops for row and column respectively
const container = document.querySelector('.container');
function createGrid(rows, columns) {
// remove previous grid
container.innerHTML = '';
container.style.display = 'flex';
container.style.flexDirection = 'column';
for (let i = 0; i < rows; i++) {
const row = document.createElement('div');
row.classList.add('gridRow');
container.appendChild(row);
for(let j = 0; j < columns; j++) {
const item = document.createElement('div');
item.classList.add('gridItem');
item.id = (i * columns) + (j + 1); // gives seperate #id for each item
row.appendChild(item);
// "hover" effect so gridItem changes color
item.addEventListener('mousemove', (event) => {
event.target.style.backgroundColor = 'black';
});
};
}
}
// create initial grid
createGrid(gridSize, gridSize);
// Button click event
btn.addEventListener('click', () => {
let newGridSize = prompt('Please enter a new grid size between 16 - 100');
// validate the new grid size
if (newGridSize >= 16 && newGridSize <= 100) {
gridSize = newGridSize;
createGrid(gridSize, gridSize);
} else {
alert('Invalid grid size! Please enter a value between 16 and 100.');
}
});