-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
45 lines (37 loc) · 1.22 KB
/
todo.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
const taskInput = document.getElementById("input");
const taskList = document.getElementById("display");
const tasks = JSON.parse(localStorage.getItem("tasks")) || [];
function addTask() {
const taskText = taskInput.value.trim();
if (taskText === "") return;
const task = { text: taskText };
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
taskInput.value = "";
displayTasks();
}
function delTask(index) {
tasks.splice(index, 1);
localStorage.setItem("tasks", JSON.stringify(tasks));
displayTasks();
}
function displayTasks() {
taskList.innerHTML = "";
tasks.forEach((task, index) => {
const li = document.createElement("li");
li.textContent = task.text;
const deleteButton = document.createElement("button");
deleteButton.innerHTML = '<i class="ri-delete-bin-line"></i>';
deleteButton.onclick = function() {
delTask(index);
};
li.appendChild(deleteButton);
taskList.appendChild(li);
});
if (tasks.length === 0) {
taskList.classList.add('hidden');
} else {
taskList.classList.remove('hidden');
}
}
displayTasks();