-
Notifications
You must be signed in to change notification settings - Fork 0
/
to do list.js
100 lines (75 loc) · 3.29 KB
/
to do list.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function addTask(inputText) {
if (!inputText) {
alert("Add something!");
return;
}
const listContainer = document.querySelector(".list-container");
listContainer.style.display = "block";
const newListElement = document.createElement("li");
newListElement.textContent = inputText;
const closeButton = document.createElement("span");
closeButton.classList.add("close-btn");
closeButton.textContent = "\u00D7";
closeButton.addEventListener("click", () => {
closeButton.parentElement.remove();
const existingList = JSON.parse(sessionStorage.getItem("taskList") || "[]");
const taskIndex = existingList.indexOf(inputText);
if (taskIndex !== -1) {
existingList.splice(taskIndex, 1);
sessionStorage.setItem("taskList", JSON.stringify(existingList));
}
if (!listContainer.querySelectorAll("li").length) {
listContainer.style.display = "none";
}
});
newListElement.append(closeButton);
document.querySelector(".list").append(newListElement);
document.querySelector("#input-box").value = "";
const existingList = JSON.parse(sessionStorage.getItem("taskList") || "[]");
existingList.push(inputText);
sessionStorage.setItem("taskList", JSON.stringify(existingList));
}
// Load the data from local storage on loading the page
window.onload = function () {
const storedList = JSON.parse(sessionStorage.getItem("taskList") || "[]");
storedList.forEach(function (listItem) {
const listContainer = document.querySelector(".list-container");
listContainer.style.display = "block";
const newListElement = document.createElement("li");
newListElement.textContent = listItem;
const closeButton = document.createElement("span");
closeButton.classList.add("close-btn");
closeButton.textContent = "\u00D7";
closeButton.addEventListener("click", function () {
closeButton.parentElement.remove();
const existingList = JSON.parse(sessionStorage.getItem("taskList") || "[]");
const taskIndex = existingList.indexOf(listItem);
if (taskIndex !== -1) {
existingList.splice(taskIndex, 1);
sessionStorage.setItem("taskList", JSON.stringify(existingList));
}
if (!listContainer.querySelectorAll("li").length) {
listContainer.style.display = "none";
}
});
newListElement.append(closeButton);
document.querySelector(".list").append(newListElement);
document.querySelector("#input-box").value = "";
});
};
// Add event listener to toggle "strikethrough" class on list item click
document.querySelector(".list").addEventListener("click", function (event) {
if (event.target.tagName === "LI") {
event.target.classList.toggle("strikethrough");
}
});
// Add event listener to the "Add" button or input box to capture input
document.querySelector("#add-button").addEventListener("click", function () {
const inputBox = document.querySelector("#input-box").value;
addTask(inputBox);
});
document.querySelector("#input-box").addEventListener("keypress", function (event) {
if (event.key === "Enter") {
addTask(this.value);
}
});