-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (33 loc) · 1.25 KB
/
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
34
35
36
37
38
39
let btn = document.querySelector("button");
let ul = document.querySelector("ul");
let inp = document.querySelector("input");
btn.addEventListener("click", function(){
let task = inp.value.trim(); // Trim the input value to remove leading/trailing whitespace
if(task === ""){
alert("Enter a value");
}
else{
let item = document.createElement("li");
item.innerText = task;
let removeButton = document.createElement("button"); // Create a remove button
removeButton.innerHTML = "\u00d7";
removeButton.onclick = function() {
item.remove(); // Remove the parent li when the button is clicked
};
item.appendChild(removeButton);
ul.appendChild(item);
inp.value = "";
}
});
ul.addEventListener('click',function(e){
if(e.target.tagName === 'LI'){
e.target.classList.toggle('check'); // Changed class name to "checked" for better clarity
}
});
// Added functionality to handle pressing enter key
inp.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
event.preventDefault();
btn.click(); // Simulate a click on the button when Enter key is pressed
}
});