-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.js
102 lines (84 loc) · 2.67 KB
/
Index.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
102
const addButton = document.getElementById("addButton");
const completeAllButton = document.getElementById("completeAllButton");
const clearButton = document.getElementById("clearButton");
const taskInput = document.getElementById("taskInput");
const taskList = document.getElementById("taskList");
const taskCount = document.getElementById("taskCount");
addButton.addEventListener("click", addTask);
completeAllButton.addEventListener("click", toggleAllCompleted);
clearButton.addEventListener("click", clearAllTasks);
taskInput.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
addTask();
}
});
function addTask() {
const taskText = taskInput.value.trim();
if (taskText !== "") {
const li = document.createElement("li");
li.innerHTML = `
<span class="check">✓</span>
<span>${taskText}</span>
<span class="delete">X</span>
`;
taskList.appendChild(li);
taskInput.value = "";
addEventListeners(li);
updateTaskCount();
}
}
function addEventListeners(li) {
const check = li.querySelector(".check");
const deleteBtn = li.querySelector(".delete");
check.addEventListener("click", toggleTask);
deleteBtn.addEventListener("click", deleteTask);
}
function toggleTask(event) {
const check = event.target;
const task = check.parentElement;
task.classList.toggle("completed");
updateTaskCount();
}
function deleteTask(event) {
const deleteBtn = event.target;
const task = deleteBtn.parentElement;
taskList.removeChild(task);
updateTaskCount();
}
function toggleAllCompleted() {
const tasks = taskList.children;
let allCompleted = true;
for (const task of tasks) {
const check = task.querySelector(".check");
const completed = task.classList.contains("completed");
if (!completed) {
allCompleted = false;
break;
}
}
for (const task of tasks) {
const check = task.querySelector(".check");
const completed = task.classList.contains("completed");
if (allCompleted) {
task.classList.remove("completed");
if (!completed) {
check.textContent = "";
}
} else {
task.classList.add("completed");
if (!completed) {
check.textContent = "✓";
}
}
}
updateTaskCount();
}
function clearAllTasks() {
taskList.innerHTML = ""; // Clear all tasks
updateTaskCount();
}
function updateTaskCount() {
const totalTasks = taskList.children.length;
const completedTasks = taskList.querySelectorAll(".completed").length;
taskCount.textContent = `${completedTasks} tasks completed out of ${totalTasks} total tasks`;
}