-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
47 lines (38 loc) · 1.1 KB
/
main.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
const form = document.getElementById('taskForm');
const list = document.getElementById('tasks');
form.addEventListener('submit', addTask);
function addTask(e) {
e.preventDefault();
const task = document.getElementById('task-input').value;
if (task) {
const newTask = document.createElement('li');
newTask.className = 'task';
const newTaskTitle = document.createTextNode(task);
const button = document.createElement('button');
button.className = 'button';
newTask.appendChild(button);
newTask.appendChild(newTaskTitle);
list.appendChild(newTask);
form.reset();
} else {
alert('Invalid task.');
}
}
tasks.addEventListener('click', modifyTask);
function modifyTask(e) {
if (e.target.tagName == 'BUTTON') {
const task = e.target.parentElement;
// Complete/uncomplete task.
if (task.className == 'task') {
e.target.className = 'button completed-button';
task.className = 'task completed-task';
} else if ((task.className = 'task completed-task')) {
e.target.className = 'button';
task.className = 'task';
}
// Delete task.
if (e.shiftKey) {
task.remove();
}
}
}