-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
35 lines (27 loc) · 932 Bytes
/
main.js
File metadata and controls
35 lines (27 loc) · 932 Bytes
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
// Function to add a task to the list
function addTask() {
const taskInput = document.getElementById("task-input");
const taskValue = taskInput.value.trim();
if (taskValue === "") {
alert("Please enter a task!");
return;
}
const taskList = document.getElementById("task-list");
// Create a new list item
const listItem = document.createElement("li");
listItem.textContent = taskValue;
// Add a complete button
listItem.addEventListener("click", () => {
listItem.classList.toggle("completed");
});
// Add a delete button
const deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.onclick = () => listItem.remove();
// Append delete button to list item
listItem.appendChild(deleteBtn);
// Append list item to task list
taskList.appendChild(listItem);
// Clear the input field
taskInput.value = "";
}