forked from BreakTos/To-do-List
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
99 lines (83 loc) · 3.17 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Function to save tasks in localStorage
function saveTasks() {
const tasks = [];
document.querySelectorAll("li").forEach(item => {
const taskText = item.querySelector("a").textContent;
const taskStartTime = item.querySelector(".start-time").textContent;
const taskEndTime = item.querySelector(".end-time").textContent;
const task = {
text: taskText,
startTime: taskStartTime,
endTime: taskEndTime,
completed: false
};
tasks.push(task);
});
localStorage.setItem("tasks", JSON.stringify(tasks));
}
// Load tasks from localStorage when page is loaded
window.onload = function () {
const savedTasks = localStorage.getItem("tasks");
if (savedTasks) {
const tasksArray = JSON.parse(savedTasks);
tasksArray.forEach(task => {
addTaskToList(task.text, task.startTime, task.endTime);
});
}
}
// Helper function to add a task to the DOM
function addTaskToList(taskText, startTime, endTime) {
const newItem = document.createElement("li");
const link = document.createElement("a");
link.innerHTML = `<strong>${taskText}</strong>`;
const startTimeSpan = document.createElement("span");
startTimeSpan.classList.add("start-time");
startTimeSpan.innerHTML = ` (Start: ${startTime})`;
const endTimeSpan = document.createElement("span");
endTimeSpan.classList.add("end-time");
endTimeSpan.innerHTML = ` (End: ${endTime})`;
const editButton = document.createElement("button");
editButton.innerHTML = "Edit";
editButton.classList.add("edit-btn");
editButton.addEventListener("click", function () {
const newTask = prompt("Edit your task:", taskText);
if (newTask) {
link.innerHTML = `<strong>${newTask}</strong>`;
saveTasks(); // Save after editing
}
});
const doneButton = document.createElement("button");
doneButton.innerHTML = "Done";
doneButton.classList.add("done-btn");
doneButton.addEventListener("click", function () {
newItem.remove();
saveTasks(); // Save after removing
});
newItem.appendChild(link);
newItem.appendChild(startTimeSpan);
newItem.appendChild(endTimeSpan);
newItem.appendChild(editButton);
newItem.appendChild(doneButton);
document.getElementById("list").appendChild(newItem);
}
// Handle form submission and add task
function handleSubmit(event) {
event.preventDefault();
const taskText = document.getElementById("taskInput").value;
const startTime = document.getElementById("startTimeInput").value;
const endTime = document.getElementById("endTimeInput").value;
if (taskText == "" || startTime == "" || endTime == "") {
alert("Please fill out all fields.");
return;
}
addTaskToList(taskText, startTime, endTime);
document.getElementById("taskInput").value = "";
document.getElementById("startTimeInput").value = "";
document.getElementById("endTimeInput").value = "";
saveTasks(); // Save after adding
}
// Clear all tasks
function clearAllTasks() {
document.getElementById("list").innerHTML = "";
localStorage.removeItem("tasks");
}