-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (56 loc) · 1.97 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Get references to the relevant HTML elements
const taskInput = document.getElementById("taskInput");
const addButton = document.getElementById("addButton");
const taskList = document.getElementById("taskList");
const tasks = JSON.parse(localStorage.getItem("tasks")) || [];
// Add an event listener to the "Add" button
addButton.addEventListener("click", addTask);
tasks.forEach(renderTask);
// Function to add a new task
function addTask() {
const taskText = taskInput.value.trim();
if (taskText !== "") {
const task = {text: taskText, completed: false };
tasks.push(task);
saveTasks();
renderTask(task);
taskInput.value = "";
}
}
function renderTask(task) {
const taskItem = document.createElement("li");
taskItem.textContent = task.text;
taskItem.classList.toggle("completed", task.completed);
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = task.completed;
checkbox.addEventListener("change", () => {
task.completed = checkbox.checked;
taskItem.classList.toggle("completed", task.completed);
saveTasks();
});
const deleteButton = document.createElement("button");
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", () => {
const taskIndex = tasks.indexOf(task);
if (taskIndex > -1) {
tasks.splice(taskIndex, 1);
saveTasks();
taskItem.remove();
}
});
taskItem.appendChild(checkbox);
taskItem.appendChild(deleteButton);
taskList.appendChild(taskItem);
}
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
// Add an event listener to the input field to handle pressing the Enter key
taskInput.addEventListener("keyup", function(event) {
// Check if the pressed key is "Enter"
if (event.key === "Enter") {
// Call the addTask function to add a new task
addTask();
}
});