-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
58 lines (54 loc) · 1.49 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
48
49
50
51
52
53
54
55
56
57
58
showTasks();
let new_task_submit = document.getElementById("new-task-submit");
new_task_submit.addEventListener("click", function (e) {
let new_task_input = document.getElementById("new-task-input");
let tasks = localStorage.getItem("tasks");
if (tasks == null) {
taskObj = [];
} else {
taskObj = JSON.parse(tasks);
}
taskObj.push(new_task_input.value);
localStorage.setItem("tasks", JSON.stringify(taskObj));
new_task_input.value = "";
console.log(taskObj);
showTasks();
});
function showTasks() {
let tasks = localStorage.getItem("tasks");
if (tasks == null) {
taskObj = [];
} else {
taskObj = JSON.parse(tasks);
}
let html = "";
taskObj.forEach(function (element, index) {
html += `
<div class="join">
<div class="content">
<p class="task">${element}</p>
</div>
<div class="actions">
<button id="${index}" onclick="deleteNode(${index})" class="delete" >Delete</button>
</div>
</div>
`;
});
let taskEle = document.getElementById("tasks");
if (taskObj != 0) {
taskEle.innerHTML = html;
} else {
taskEle.innerHTML = `Nothing to show! Use "Add a Task" section above to add tasks.`;
}
}
function deleteNode(index) {
let tasks = localStorage.getItem("tasks");
if (tasks == null) {
taskObj = [];
} else {
taskObj = JSON.parse(tasks);
}
taskObj.splice(index, 1);
localStorage.setItem("tasks", JSON.stringify(taskObj));
showTasks();
}