-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
57 lines (48 loc) · 1.45 KB
/
todo.js
File metadata and controls
57 lines (48 loc) · 1.45 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
document.addEventListener("DOMContentLoaded", showList);
const inputBox = document.getElementById("input-box");
const listContainer = document.getElementById("list-container");
function addTask() {
const task = inputBox.value.trim();
if (!task) {
alert("Please enter a task!");
return;
}
const li = document.createElement("li");
li.textContent = task;
const span = document.createElement("span");
span.innerHTML = "\u00D7";
span.setAttribute("aria-label", "Remove task");
span.onclick = () => {
li.remove();
saveData();
};
li.appendChild(span);
// Mark task as completed
li.onclick = (e) => {
if (e.target.tagName !== "SPAN") {
li.classList.toggle("completed");
saveData();
}
};
listContainer.appendChild(li);
inputBox.value = "";
saveData();
}
function saveData() {
localStorage.setItem("tasks", listContainer.innerHTML);
}
function showList() {
listContainer.innerHTML = localStorage.getItem("tasks") || "";
Array.from(listContainer.children).forEach((li) => {
li.querySelector("span").onclick = () => {
li.remove();
saveData();
};
li.onclick = (e) => {
if (e.target.tagName !== "SPAN") {
li.classList.toggle("completed");
saveData();
}
};
});
}