-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
32 lines (26 loc) · 1019 Bytes
/
tasks.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
document.addEventListener("DOMContentLoaded", () => {
const taskInput = document.getElementById("taskInput");
const addTaskBtn = document.getElementById("addTaskBtn");
const taskList = document.getElementById("taskList");
// Function to add a new task
addTaskBtn.addEventListener("click", () => {
const taskText = taskInput.value.trim();
if (!taskText) return;
// Create a list item
const listItem = document.createElement("li");
const taskTextSpan = document.createElement("span");
taskTextSpan.textContent = taskText;
listItem.appendChild(taskTextSpan);
// Add a delete button
const deleteBtn = document.createElement("button");
deleteBtn.className = "delete-btn";
deleteBtn.textContent = "Delete";
deleteBtn.addEventListener("click", () => {
taskList.removeChild(listItem);
});
listItem.appendChild(deleteBtn);
taskList.appendChild(listItem);
// Clear the input
taskInput.value = "";
});
});