-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (51 loc) · 1.77 KB
/
Copy pathscript.js
File metadata and controls
61 lines (51 loc) · 1.77 KB
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
let taskList = document.querySelector('ul');
let addTask = document.querySelector('#add-task-button');
let Store = [];
function removeAllChildNodes(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
const deleteTask = (id) => {
let newStore = Store.filter(task => task.id !== id);
localStorage.setItem("tasks", JSON.stringify(newStore));
displayToDoList();
};
const doneTask = (id) => {
let newStore = Store.map(task => {
if(task.id === id) {
task.done = !task.done;
return task;
}
return task;
});
localStorage.setItem("tasks", JSON.stringify(newStore));
displayToDoList();
}
const displayToDoList = () => {
Store = JSON.parse(localStorage.getItem('tasks')) || [];
removeAllChildNodes(taskList);
Store.map(task => {
let newTask = document.createElement('li');
newTask.setAttribute('key', task.id);
newTask.innerHTML = `
<input type="checkbox" onchange="doneTask((Number(this.parentNode.getAttribute('key'))))">
<span class="task ${task.done && "selectTask"}">${task.text}</span>
<button class="delete-btn" onclick="return this.parentNode.remove();">Delete</button>
<button class="delete-btn" onclick="deleteTask(Number(this.parentNode.getAttribute('key')));">Delete</button>`;
taskList.append(newTask);
});
};
addTask.addEventListener("click", () => {
let taskText = document.querySelector('#input-task');
let newTask = {
id: Number(Store.length) + 1,
text: taskText.value,
done: false,
};
Store.push(newTask);
taskText.value = '';
localStorage.setItem("tasks", JSON.stringify(Store));
displayToDoList();
});
displayToDoList();