-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (64 loc) · 2.28 KB
/
Copy pathscript.js
File metadata and controls
82 lines (64 loc) · 2.28 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
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
// Load saved tasks on page load
window.onload = function () {
const saved = JSON.parse(localStorage.getItem("todoList")) || [];
saved.forEach(item => additem(item.text, item.completed));
};
function saveToLocalStorage() {
let items = document.querySelectorAll("#ul li");
const data = Array.from(items).map(li => {
return {
text: li.childNodes[0].textContent.trim(),
completed: li.classList.contains("completed")
};
});
localStorage.setItem("todoList", JSON.stringify(data));
}
function additem(textFromStorage = null, isCompleted = false) {
const input = document.getElementById("id");
const value = textFromStorage || input.value.trim();
if (value !== "") {
const li = document.createElement("li");
li.textContent = value;
const deletebtn = document.createElement("button");
deletebtn.textContent = "x";
deletebtn.className = "ahm";
deletebtn.onclick = function (e) {
e.stopPropagation(); // prevent toggle
li.remove();
saveToLocalStorage();
};
li.onclick = function () {
if (li.classList.contains("completed")) {
li.classList.remove("completed");
li.classList.add("undo");
} else {
li.classList.add("completed");
li.classList.remove("undo");
}
saveToLocalStorage();
};
li.appendChild(deletebtn);
if (isCompleted) {
li.classList.add("completed");
}
document.getElementById("ul").appendChild(li);
if (!textFromStorage) {
input.value = "";
}
saveToLocalStorage();
const clear = document.getElementById('clear');
// Get reference to the clear button
const clearBtn = document.getElementById('clear');
const taskList = document.getElementById('ul'); // The <ul> where tasks are added
// Clear all tasks
clearBtn.addEventListener('click', () => {
if (confirm("Are you sure you want to clear all tasks?")) {
localStorage.removeItem('tasks'); // Clear localStorage
taskList.innerHTML = ''; // Clear the task list from the page
saveToLocalStorage();
// Optional: If you're using an array to track tasks
// tasks = [];
}
});
}
}