-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
78 lines (67 loc) · 2.26 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const inputBox = document.getElementById("input-box");
const listContainer = document.getElementById("list-container");
function addTask() {
if (inputBox.value === "") {
alert("You must write something!");
} else {
let li = document.createElement("li");
let undoneImg = document.createElement("img");
undoneImg.src = "undone.png";
undoneImg.id = "mark-list";
let crossImg = document.createElement("img");
crossImg.src = "cross.png";
crossImg.id = "delet-list";
let task = "pending";
li.innerHTML = inputBox.value;
li.appendChild(undoneImg);
li.appendChild(crossImg);
listContainer.appendChild(li);
saveData();
// Add event listeners
addEventListenersToTask(li, undoneImg, crossImg, task);
}
inputBox.value = "";
}
// Add event listeners to task
function addEventListenersToTask(li, undoneImg, crossImg, task) {
crossImg.addEventListener("click", () => {
listContainer.removeChild(li);
saveData();
});
undoneImg.addEventListener("click", () => {
if (task === "pending") {
undoneImg.src = "done.png";
li.classList.add("done-text");
task = "done";
} else {
undoneImg.src = "undone.png";
li.classList.remove("done-text");
task = "pending";
}
saveData();
});
}
// Save the data
function saveData() {
localStorage.setItem("data", listContainer.innerHTML);
}
// Show the data
function showData() {
listContainer.innerHTML = localStorage.getItem("data");
if (listContainer.innerHTML !== "") {
// Re-add event listeners for each task after loading data
listContainer.querySelectorAll("li").forEach(li => {
let undoneImg = li.querySelector("#mark-list");
let crossImg = li.querySelector("#delet-list");
let task = undoneImg.src.includes("done.png") ? "done" : "pending";
addEventListenersToTask(li, undoneImg, crossImg, task);
});
}
}
// Clear saved data
function clearData() {
localStorage.clear();
listContainer.innerHTML = "";
}
// Call showData function to display tasks after refresh
showData();