-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
80 lines (65 loc) · 1.81 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
(function todo() {
const addTasks = document.querySelector(".add-tasks");
const tasksList = document.querySelector(".tasks");
const tasks = JSON.parse(localStorage.getItem("tasks")) || [];
function populate() {
tasksList.innerHTML = tasks
.map(task => {
let status = task.done ? "done" : " ";
return `
<li>
<label class=${status}>${task.text}</label>
<span class="del">x</span>
</li>`;
})
.join("");
}
function addTask(e) {
e.preventDefault();
const taskText = this.querySelector("[type=text]").value;
const task = {
text: taskText,
done: false
};
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
populate();
this.reset();
}
function handleClick(e) {
if (!e.target.matches("span") && !e.target.matches("label")) {
return;
}
if (e.target.classList.contains("del")) {
let labelText = e.target.previousElementSibling.innerHTML;
let index = false;
for (let i = 0; i < tasks.length; i++) {
if (tasks[i].text === labelText) {
index = i;
}
}
tasks.splice(index, 1);
populate();
}
if (e.target.matches("label")) {
e.target.classList.toggle("done");
let labelText = e.target.innerHTML;
tasks.forEach(task => {
if (task.text === labelText) {
task.done = !task.done;
}
});
}
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function hideFooter(e) {
if (!e.target.matches("footer")) {
return;
}
e.target.style.display = "none";
}
addTasks.addEventListener("submit", addTask);
tasksList.addEventListener("click", handleClick);
document.querySelector(".footer").addEventListener("click", hideFooter);
populate();
})();