-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
33 lines (26 loc) · 944 Bytes
/
script.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
let todoInput = document.querySelector(".todo-input");
let btn = document.querySelector(".btn");
let todos = document.querySelector(".todos");
// Add event listener only once
btn.addEventListener("click", function () {
let todovalue = todoInput.value.trim();
if (todovalue === "") {
console.log("Please enter a valid todo item.");
return;
}
let li = document.createElement("li");
let delBtn = document.createElement("button");
delBtn.innerHTML = "Delete";
// Add delete functionality
delBtn.addEventListener("click", function () {
li.remove(); // Remove the entire list item
delBtn.remove();
});
// Set up the list item content
li.textContent = todovalue;
// Add both elements to the DOM
todos.appendChild(li);
todos.appendChild(delBtn);
// Clear input field after adding todo
todoInput.value = "";
});