-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (62 loc) · 1.71 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Create boxes
function createBoard(width) {
var board = document.getElementById("board");
var boardWidth = parseInt(width);
if (isNaN(boardWidth)) {
console.warn("String received");
} else {
board.style.width = `${boardWidth}px`;
board.style.height = `${boardWidth}px`;
var boxLength = Math.sqrt(boardWidth);
console.log(`Board width ${boardWidth}`);
console.log(`Box length ${boxLength}`);
for (var i = 0; i < boxLength; i++) {
for (var j = 0; j < boxLength; j++) {
var box = document.createElement("div");
box.className = "box";
box.style.width = `${boxLength}px`;
box.style.height = `${boxLength}px`;
board.append(box);
}
}
drawingListener();
}
}
function drawingListener() {
var boxes = document.getElementsByClassName("box");
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener(
"mouseover",
function (event) {
// highlight the mouseover target
console.log(event.target);
event.target.style.background = "orange";
// reset the color after a short delay
setTimeout(function () {
event.target.style.background = "";
}, 9500);
},
false
);
}
}
var promptButton = document.getElementById("prompt");
promptButton.addEventListener(
"click",
function (event) {
// highlight the mouseover target
deleteBoxes();
let width = prompt("Please enter desired width");
createBoard(width);
},
false
);
function deleteBoxes() {
var e = document.querySelector("#board");
//e.firstElementChild can be used.
var child = e.lastElementChild;
while (child) {
e.removeChild(child);
child = e.lastElementChild;
}
}