-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (62 loc) · 2.16 KB
/
Copy pathscript.js
File metadata and controls
74 lines (62 loc) · 2.16 KB
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
const inputBox = document.getElementById("input-box");
const listContainer = document.getElementById("list-container");
const completedCounter = document.getElementById("completed-counter");
const uncompletedCounter = document.getElementById("uncompleted-counter");
function updateCounters() {
const completedTasks = document.querySelectorAll(".completed").length;
const uncompletedTasks = document.querySelectorAll("li:not(.completed)").length;
completedCounter.textContent = completedTasks;
uncompletedCounter.textContent = uncompletedTasks;
}
function addTask() {
const task = inputBox.value.trim();
if (!task) {
alert("Please write down a task");
console.log("no task added");
return;
}
const li = document.createElement("li");
li.innerHTML = `
<label>
<input type="checkbox">
<span>${task}</span>
</label>
<span class="edit-btn">Edit</span>
<span class="delete-btn">Delete</span>
`;
listContainer.appendChild(li);
// clear the input field
inputBox.value = " ";
// attach event listeners to the new task
const checkbox = li.querySelector("input");
const editBtn = li.querySelector(".edit-btn");
const taskSpan = li.querySelector("span");
const deleteBtn = li.querySelector(".delete-btn");
// strike out the completed task
checkbox.addEventListener("click", function () {
li.classList.toggle("completed", checkbox.checked);
updateCounters();
});
editBtn.addEventListener("click", function () {
const update = prompt("Edit task:", taskSpan.textContent);
if (update !== null) {
taskSpan.textContent = update;
li.classList.remove("completed");
checkbox.checked = false;
updateCounters();
}
});
deleteBtn.addEventListener("click", function () {
if (confirm("Are you sure you want to delete this task?")) {
li.remove();
updateCounters();
}
});
updateCounters();
}
// add task when pressing Enter key
inputBox.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
addTask();
}
});