forked from Divyansh013/Todo-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
87 lines (75 loc) · 2.56 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
const todoForm = document.querySelector(".todo-form")
const todoInput = document.querySelector("input");
const todoCollection = document.querySelector(".todo-collection");
todoForm.addEventListener("submit", addTodo);
function addTodo(e)
{
if (todoInput.value=== "")
{
alert("You cannot enter empty value");
}
else
{
const li = document.createElement("li");
const todoTitle = document.createElement("span");
const editableInput = document.createElement("input");
const editButton = document.createElement("button");
const saveButton = document.createElement("button");
const deleteButton = document.createElement("button");
li.classList.add("todo-collection__item");
todoTitle.classList.add("todo-collection__item__title");
todoTitle.innerText = todoInput.value;
editableInput.classList.add("input");
editableInput.classList.add("input--todo");
editableInput.classList.add("hidden");
editableInput.type = "text";
editableInput.value = todoInput.value;
editButton.classList.add("button");
editButton.classList.add("button--todo");
editButton.classList.add("button--edit");
editButton.innerText = "Edit";
saveButton.classList.add("button");
saveButton.classList.add("button--todo");
saveButton.classList.add("button--save");
saveButton.classList.add("hidden");
saveButton.innerText = "Save";
deleteButton.classList.add("button");
deleteButton.classList.add("button--todo");
deleteButton.classList.add("button--delete");
deleteButton.innerText = "Delete";
li.appendChild(todoTitle);
li.appendChild(editableInput);
li.appendChild(editButton);
li.appendChild(saveButton);
li.appendChild(deleteButton);
todoCollection.appendChild(li);
function toggleTodoEditForm() {
if (editableInput.value=== "")
{
alert("You cannot enter empty value while editing");
}
else
{
todoTitle.classList.toggle("hidden");
editableInput.classList.toggle("hidden");
editButton.classList.toggle("hidden");
saveButton.classList.toggle("hidden");
}
}
editButton.addEventListener("click", () => {
toggleTodoEditForm();
editableInput.focus();
});
saveButton.addEventListener("click", () => {
todoTitle.innerText = editableInput.value;
toggleTodoEditForm();
});
deleteButton.addEventListener("click", () => {
todoCollection.removeChild(li);
});
}
// clear input
// IF we will not give below then at input place it will not be cleared.
todoInput.value = "";
e.preventDefault();
}