-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (45 loc) · 1.61 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
window.addEventListener("load", function() {
const form = document.getElementById("new-task-form");
form.addEventListener("submit", addTask);
});
function addTask(e) {
e.preventDefault();
// get the task text and clear input
const input = document.getElementById("new-task");
const task = input.value;
input.value = "";
// create and parse html for new list item
let html = `<li>${task}</li>`;
const el = document.createElement("div");
el.innerHTML = html;
const listItem = el.childNodes[0];
listItem.addEventListener("click", completeTask);
// append item to list
const list = document.getElementById("task-list");
list.append(listItem);
updateTaskTotal(list.childNodes.length);
}
function completeTask(e) {
const listItem = e.target;
const list = document.getElementById("task-list");
const completedList = document.getElementById("completed-list");
listItem.remove();
listItem.removeEventListener("click", completeTask);
completedList.append(listItem);
listItem.addEventListener("click", uncompleteTask);
updateTaskTotal(list.childNodes.length);
}
function uncompleteTask(e) {
const listItem = e.target;
const list = document.getElementById("task-list");
const completedList = document.getElementById("completed-list");
listItem.remove();
listItem.removeEventListener("click", uncompleteTask);
list.append(listItem);
listItem.addEventListener("click", completeTask);
updateTaskTotal(list.childNodes.length);
}
function updateTaskTotal(total) {
const totalContainer = document.getElementById("task-total");
totalContainer.innerHTML = `- ${total} TODOs total`;
}