-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (60 loc) · 1.72 KB
/
script.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
const table = document.getElementsByTagName("table")[0];
const select = document.getElementsByTagName("select")[0];
const button = document.getElementsByTagName("button")[0];
let chosenColor = "maize";
button.addEventListener("click", makeMultipleRows);
table.addEventListener("click", colorize);
table.addEventListener("mousedown", addMouseOver);
table.addEventListener("mouseup", removeMouseOver);
select.addEventListener("change", pickColor);
function makeRow() {
const row = document.createElement("tr");
for (let i = 0; i < 70; i++) {
const td = document.createElement("td");
row.appendChild(td);
}
table.appendChild(row);
}
function removeElement() {
while (table.firstChild) {
table.removeChild(table.lastChild);
}
}
function makeMultipleRows() {
if (table.children.length) {
console.log("table.children.length=>", table.children.length);
removeElement();
button.innerText = "Start";
} else {
for (let i = 0; i < 10; i++) {
makeRow();
console.log("table.children.length", table.children.length);
}
button.innerText = "Reset";
}
}
function colorize(event) {
const target = event.target;
if (target.tagName !== "TD") {
return;
}
if (target.className === chosenColor) {
target.className = "";
} else {
target.className = chosenColor;
}
}
function pickColor(event) {
chosenColor = event.target.value;
}
function addMouseOver() {
table.addEventListener("mouseover", colorize);
}
function removeMouseOver() {
table.removeEventListener("mouseover", colorize);
}
// function removeElement(elementId) {
// // Removes an element from the document
// var element = document.getElementById(elementId);
// element.parentNode.removeChild(element);
// }