-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (49 loc) · 1.94 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
$(document).ready(function () {
$(".datepicker").datepicker();
$("#add").click(function () {
const taskText = $("#task").val();
const dueDate = $("#date").val();
if (taskText.trim() !== "") {
const taskItem = $("<li>");
taskItem.html(`<span>${taskText}</span> <span class="due-date">${dueDate}</span> <button class="delete">Delete</button> <button class="complete">Complete</button>`);
$("#task-list").append(taskItem);
$("#task").val("");
$("#date").val("");
}
});
$("#task-list").on("click", ".delete", function () {
$(this).parent().remove();
});
$("#task-list").on("click", ".complete", function () {
$(this).parent().toggleClass("completed");
});
// Save tasks to local storage
function saveTasksToLocalStorage() {
const tasks = [];
$("#task-list li").each(function () {
tasks.push({
text: $(this).find("span").eq(0).text(),
dueDate: $(this).find(".due-date").text(),
completed: $(this).hasClass("completed"),
});
});
localStorage.setItem("tasks", JSON.stringify(tasks));
}
// Load tasks from local storage
function loadTasksFromLocalStorage() {
const tasks = JSON.parse(localStorage.getItem("tasks")) || [];
tasks.forEach(function (task) {
const taskItem = $("<li>");
if (task.completed) {
taskItem.addClass("completed");
}
taskItem.html(`<span>${task.text}</span> <span class="due-date">${task.dueDate}</span> <button class="delete">Delete</button> <button class="complete">Complete</button>`);
$("#task-list").append(taskItem);
});
}
loadTasksFromLocalStorage();
// Save tasks when the page unloads
$(window).on("beforeunload", function () {
saveTasksToLocalStorage();
});
});