-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (39 loc) · 1.24 KB
/
script.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
const taskInput = document.getElementById("taskInput");
const taskList = document.getElementById("taskList");
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 deleteTask(index) {
tasks.splice(index, 1);
localStorage.setItem("tasks", JSON.stringify(tasks));
displayTasks();
}
function editTask(index) {
const newTaskText = prompt("Edit the Task: ", tasks[index].text);
if (newTaskText !== null) {
tasks[index].text = newTaskText;
localStorage.setItem("tasks", JSON.stringify(tasks));
displayTasks();
}
}
function displayTasks() {
taskList.innerHTML = "";
tasks.forEach((task, index) => {
const li = document.createElement("li");
li.innerHTML = `
<span>${task.text}</span>
<hr>
<button class="edit-button" onclick="editTask(${index})">Edit</button>
<button class="delete-button" onclick="deleteTask(${index})">Delete</button>
`;
taskList.appendChild(li);
});
}
displayTasks();