-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
101 lines (84 loc) · 2.72 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
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
const todoList = [];
function displayTodoList() {
const todoListElement = document.querySelector("ul");
todoListElement.innerHTML = "";
for (let i = 0; i < todoList.length; i++) {
const todoItem = todoList[i];
const li = document.createElement("li");
li.textContent = todoItem.task;
if (todoItem.completed) {
li.classList.add("completed");
}
const actionsDiv = document.createElement("div");
actionsDiv.classList.add("actions");
const editButton = document.createElement("button");
editButton.textContent = "Edit";
editButton.classList.add("edit");
editButton.addEventListener("click", () => {
const newTask = prompt("Enter new task:", todoItem.task);
if (newTask !== null && newTask.trim() !== "") {
editTodoItem(i, newTask.trim());
}
});
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.classList.add("delete");
deleteButton.addEventListener("click", () => {
deleteTodoItem(i);
});
actionsDiv.appendChild(editButton);
actionsDiv.appendChild(deleteButton);
li.appendChild(actionsDiv);
todoListElement.appendChild(li);
}
}
function addTodoItem(task) {
const todoItem = {
task: task,
completed: false,
};
todoList.push(todoItem);
displayTodoList();
}
function deleteTodoItem(index) {
todoList.splice(index, 1);
displayTodoList();
}
function completeTodoItem(index) {
todoList[index].completed = true;
displayTodoList();
}
function editTodoItem(index, newTask) {
todoList[index].task = newTask;
displayTodoList();
}
const form = document.querySelector("form");
const input = form.querySelector("input");
form.addEventListener("submit", (event) => {
event.preventDefault();
const task = input.value.trim();
if (task !== "") {
addTodoItem(task);
input.value = "";
}
});
const todoListElement = document.querySelector("ul");
todoListElement.addEventListener("click", (event) => {
if (event.target.tagName === "LI") {
const li = event.target;
const index = Array.from(todoListElement.children).indexOf(li);
completeTodoItem(index);
} else if (event.target.classList.contains("delete")) {
const li = event.target.parentElement.parentElement;
const index = Array.from(todoListElement.children).indexOf(li);
deleteTodoItem(index);
} else if (event.target.classList.contains("edit")) {
const li = event.target.parentElement.parentElement;
const index = Array.from(todoListElement.children).indexOf(li);
const todoItem = todoList[index];
const newTask = prompt("Enter new task:", todoItem.task);
if (newTask !== null && newTask.trim() !== "") {
editTodoItem(index, newTask.trim());
}
}
});