-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (76 loc) · 3.06 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const input = document.querySelector('#task');
const task = document.querySelector('#button_task');
const group = document.querySelector('#button_group');
function delButton() {
let tasks = document.querySelector('#task_items');
let tasksDel = document.querySelector('.new-task');
tasks.removeChild(tasksDel);
}
function delButtonGroup() {
let tasks = document.querySelector('#task_items');
let tasksDelGroup = document.querySelector('.new-task-group');
tasks.removeChild(tasksDelGroup);
}
task.addEventListener('click', () => {
const tasks = document.querySelector('#task_items');
const newTR = document.createElement('tr');
const newTD = document.createElement('td');
const newButton = document.createElement('button');
newTR.classList.add('new-task');
newButton.classList.add('delete');
newButton.id = 'button_delete';
newButton.textContent = "del";
newButton.addEventListener('click', delButton);
newTD.textContent = input.value;
tasks.appendChild(newTR);
newTR.appendChild(newTD);
newTD.appendChild(newButton);
input.value = "";
input.focus();
});
// Shorter code using innerHTML instead of createElement, but I haven't figured out how to include the addEventListener for the new delete button
// task.addEventListener('click', () => {
// const tasks = document.querySelector('#task_items');
// const newTR = document.createElement('tr');
// newTR.classList.add('new-task');
// newTR.innerHTML = '<td>' + input.value + '<button class="delete" id="button_delete">del</button>' + '</td>';
// tasks.appendChild(newTR);
// input.value = "";
// input.focus();
// });
group.addEventListener('click', () => {
const tasks = document.querySelector('#task_items');
const newTR = document.createElement('tr');
const newTH = document.createElement('th');
const newButton = document.createElement('button');
newTR.classList.add('new-task-group');
newButton.classList.add('delete');
newButton.id = 'button_delete_group';
newButton.textContent = "del";
newButton.addEventListener('click', delButtonGroup);
newTH.textContent = input.value;
tasks.appendChild(newTR);
newTR.appendChild(newTH);
newTH.appendChild(newButton);
input.value = "";
input.focus();
});
// Shorter code using innerHTML instead of createElement, but I haven't figured out how to include the addEventListener for the new delete button
// group.addEventListener('click', () => {
// const tasks = document.querySelector('#task_items');
// const newTR = document.createElement('tr');
// newTR.innerHTML = '<th>' + input.value + '<button class="delete" id="button_delete">del</button>' + '</th>';
// tasks.appendChild(newTR);
// input.value = "";
// input.focus();
// });
document.addEventListener('DOMContentLoaded', () => {
let del = document.querySelector('#button_delete');
if (del) {
del.addEventListener('click', function () {
console.log('Button clicked!');
});
} else {
console.error("'#button_delete' not found.");
}
});