-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (85 loc) · 2.74 KB
/
script.js
File metadata and controls
102 lines (85 loc) · 2.74 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
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
101
102
const addtask = document.getElementById("todoadd");
const addbutton = document.getElementById("addbtn");
const ul = document.getElementById("todolist");
const remaining = document.getElementById("remains");
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
tasks.forEach((task) => {
renderTask(task);
updateRemaining();
});
function updateRemaining() {
let count = 0;
tasks.forEach((task) => {
if (!task.completed) count++;
});
remaining.textContent = `Your remaining todos: ${count}`;
}
function addingTask() {
const tasktest = addtask.value.trim(); // To remove extra spaces
if (tasktest === "") return; // Empty entry
const newtask = {
id: Date.now(),
text: tasktest,
completed: false,
}; // Create object with metadata (id, task itself, completion)
tasks.push(newtask); // add to array Tasks
saveTasks();
updateRemaining();
renderTask(newtask);
addtask.value = "";
}
addtask.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
addingTask();
}
});
addbutton.addEventListener("click", () => {
addingTask();
});
function renderTask(task) {
const li = document.createElement("li");
li.setAttribute("data-id", task.id);
li.className =
"flex items-center justify-between border border-[#C5C5C5] rounded-xl px-3 py-2";
li.innerHTML = `
<div class="flex items-center gap-2">
<input type="checkbox" />
<span class="task-text">${task.text}</span>
</div>
<button class="hover:font-bold transition">✕</button>
`;
const taskDiv = li.querySelector("div");
const checkbox = li.querySelector("input[type='checkbox']");
const span = taskDiv.querySelector("span");
ul.appendChild(li);
li.addEventListener("click", (e) => {
if (e.target.tagName === "BUTTON") {
tasks = tasks.filter((taskin) => taskin.id !== task.id);
li.remove();
saveTasks();
updateRemaining();
return;
}
if (e.target !== checkbox) {
checkbox.checked = !checkbox.checked;
}
span.classList.toggle("line-through", checkbox.checked);
span.classList.toggle("text-gray-500", checkbox.checked);
task.completed = !task.completed;
saveTasks();
updateRemaining();
});
if (task.completed) {
checkbox.checked = !checkbox.checked;
span.classList.toggle("line-through", checkbox.checked);
span.classList.toggle("text-gray-500", checkbox.checked);
}
// checkbox.addEventListener("change", () => {
// span.classList.toggle("line-through", checkbox.checked);
// span.classList.toggle("text-gray-500", checkbox.checked);
// });
console.log(task.text); // remove later
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks)); //setItem takes only string values, so we convert tasks into json and stringify
}