-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
58 lines (48 loc) · 1.59 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
const myTable = document.getElementById("mainTable")
let myColor = 'red'
function makeRow(paramNum) {
const newRow = document.createElement('tr');
myTable.appendChild(newRow);
for (let i=0; i<paramNum; i++) {
const newCell = document.createElement('td');
newRow.appendChild(newCell);
}
}
const rowButton = document.getElementById('add-row')
rowButton.addEventListener('click', function() {
makeRow(10);
});
const select = document.getElementsByTagName('select')[0]
select.addEventListener('change', function(event) {
console.log(event.target.value)
myColor = event.target.value
})
const clearButton = document.getElementById("clear");
clearButton.addEventListener('click', clearCanvas)
function clearCanvas() {
const tableArray = myTable.children;
for (let i=0; i<tableArray.length; i++) {
const row = tableArray[i].children
for (let j=0; j<row.length; j++) {
row[j].classList = [];
}
}
}
function colorize(clickEvent) {
if (clickEvent.target.matches('td')) {
// console.log(clickEvent.target.classList)
if (clickEvent.target.classList.length) {
// console.log(clickEvent.target.classList)
clickEvent.target.classList = []
} else {
clickEvent.target.classList.add(myColor)
}
}
}
myTable.addEventListener('click', colorize)
document.addEventListener('mousedown', function(event) {
myTable.addEventListener('mouseover', colorize);
})
document.addEventListener('mouseup', function(event) {
myTable.removeEventListener('mouseover', colorize);
})