-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (57 loc) · 2.19 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
const todoList = document.getElementById("todo-list");
const newItemPanel = document.getElementById("new-item");
const editItemPanel = document.getElementById("edit-item");
const newTodoInput = document.getElementById("new-todo-item-title");
const newTodoButton = document.getElementById("new-todo-item-add");
const editTodoInput = document.getElementById("edit-todo-item-title");
const editTodoConfirmButton = document.getElementById("edit-todo-item-confirm");
const editTodoCancelButton = document.getElementById("edit-todo-item-cancel");
let itemBeingEdited = null;
function addTodoItem() {
const title = newTodoInput.value.trim();
if (title === "") return;
const listItem = document.createElement("li");
const titleSpan = document.createElement("span");
titleSpan.textContent = title;
const editButton = document.createElement("button");
editButton.textContent = "Edit";
editButton.onclick = () => enterEditMode(listItem);
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.onclick = () => deleteTodoItem(listItem);
listItem.appendChild(titleSpan);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
todoList.appendChild(listItem);
newTodoInput.value = "";
}
function enterEditMode(listItem) {
itemBeingEdited = listItem;
const currentTitle = listItem.querySelector("span").textContent;
editTodoInput.value = currentTitle;
newItemPanel.hidden = true;
editItemPanel.hidden = false;
}
function confirmEdit() {
if (itemBeingEdited) {
const updatedTitle = editTodoInput.value.trim();
if (updatedTitle !== "") {
itemBeingEdited.querySelector("span").textContent = updatedTitle;
}
}
exitEditMode();
}
function cancelEdit() {
exitEditMode();
}
function exitEditMode() {
itemBeingEdited = null;
newItemPanel.hidden = false;
editItemPanel.hidden = true;
}
function deleteTodoItem(listItem) {
todoList.removeChild(listItem);
}
newTodoButton.addEventListener("click", addTodoItem);
editTodoConfirmButton.addEventListener("click", confirmEdit);
editTodoCancelButton.addEventListener("click", cancelEdit);