-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (46 loc) · 1.7 KB
/
script.js
File metadata and controls
54 lines (46 loc) · 1.7 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
const newTask = document.getElementById("input");
const addTask = document.getElementById("add-btn");
const taskList = document.getElementById("task-list");
// Add a task to the list when button is clicked
addTask.addEventListener("click", () => {
addToList();
});
// Add a task to the list when Enter key pressed
newTask.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
addToList();
}
});
function addToList() {
let text = newTask.value;
if (text) {
// Create the listItems
const listItem = document.createElement("li");
listItem.className = "task-item";
const checkbox = document.createElement("button");
checkbox.className = "btn-check";
checkbox.innerHTML = `<i class="fa-solid fa-check"><i/>`;
const span = document.createElement("span");
span.className = "text";
span.textContent = text;
const closeButton = document.createElement("button");
closeButton.className = "btn-close";
closeButton.innerHTML = `<i class="fa-solid fa-xmark"><i/>`;
// Append the buttons and text to the list
listItem.appendChild(checkbox);
listItem.appendChild(span);
listItem.appendChild(closeButton);
// Append the listItem to the task list
taskList.appendChild(listItem);
// Clear input field
newTask.value = "";
// Add functionality for close button click
closeButton.addEventListener("click", () => {
taskList.removeChild(listItem);
});
// Add functionality for checkbox click (optional)
checkbox.addEventListener("click", () => {
listItem.classList.toggle("active");
});
}
}