-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.js
93 lines (81 loc) · 2.78 KB
/
todolist.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
let todoItemsContainer = document.getElementById("todoItemsContainer");
let addTodoButton = document.getElementById("addTodoButton");
let todoList = [{
text: "Programming",
uniqueNo: 1
},
{
text: "Drinking Water",
uniqueNo: 2
},
{
text: "Exercise",
uniqueNo: 3
}
];
addTodoButton.onclick = function() {
onAddTodo();
}
function onTodoStatusChange(checkboxId, labelId) {
let checkboxElement = document.getElementById(checkboxId);
let labelElement = document.getElementById(labelId);
labelElement.classList.toggle("checked");
}
function ondeleteTodo(todoId) {
let todoElement = document.getElementById(todoId);
todoItemsContainer.removeChild(todoElement);
}
function createandAppendTODO(todo) {
let checkboxId = "checkbox" + todo.uniqueNo;
let labelId = "label" + todo.uniqueNo;
let todoId = "todo" + todo.uniqueNo;
let todoElement = document.createElement("li");
todoElement.classList.add("todo-item-container", "d-flex", "flex-row");
todoItemsContainer.appendChild(todoElement);
todoElement.id = todoId;
let inputElement = document.createElement("input");
inputElement.type = "checkbox";
inputElement.id = checkboxId;
inputElement.classList.add("checkbox-input");
todoElement.appendChild(inputElement);
inputElement.onclick = function() {
onTodoStatusChange(checkboxId, labelId);
};
let labelContainer = document.createElement("div");
labelContainer.classList.add("label-container", "d-flex", "flex-row");
todoElement.appendChild(labelContainer);
let labelElement = document.createElement("label");
labelElement.setAttribute("for", checkboxId);
labelElement.classList.add("checkbox-label");
labelElement.textContent = todo.text;
labelContainer.appendChild(labelElement);
labelElement.id = labelId;
let deleteIconContainer = document.createElement("div");
deleteIconContainer.classList.add("delete-icon-container");
labelContainer.appendChild(deleteIconContainer);
let deleteIcon = document.createElement("i");
deleteIcon.classList.add("far", "fa-trash-alt", "delete-icon");
deleteIcon.onclick = function() {
ondeleteTodo(todoId);
}
deleteIconContainer.appendChild(deleteIcon);
}
function onAddTodo() {
let todosCount = todoList.length;
let userInputElement = document.getElementById("todoUserInput");
let userInputValue = userInputElement.value;
if (userInputValue === "") {
alert("Enter Valid Text");
return;
}
todosCount = todosCount + 1;
let newTodo = {
text: userInputValue,
uniqueNo: todosCount
}
createandAppendTODO(newTodo);
userInputElement.value = "";
}
for (let eachTodo of todoList) {
createandAppendTODO(eachTodo);
}