-
Notifications
You must be signed in to change notification settings - Fork 44
/
script.js
101 lines (82 loc) · 2.83 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
100
101
const inputtdl = document.querySelector('.textarea');
const buttontdl = document.querySelector('.buttoninput');
const listtdl = document.querySelector('.todolist');
// Load todos from localStorage when the page loads
document.addEventListener('DOMContentLoaded', loadTodoLists);
// Event Listeners
buttontdl.addEventListener('click', clickButton);
listtdl.addEventListener('click', okdel);
// Function to handle button click
function clickButton(e) {
e.preventDefault();
addTodo();
}
// Function to add a new todo item
function addTodo() {
if (inputtdl.value === '') return;
const todo = {
text: inputtdl.value,
id: Date.now()
};
createTodoElement(todo);
saveTodoList(todo);
inputtdl.value = '';
}
// Function to create todo element and append to list
function createTodoElement(todo) {
const itemall = document.createElement('div');
itemall.classList.add('itemall');
itemall.setAttribute('data-id', todo.id);
const item = document.createElement('p');
item.classList.add('item');
item.innerText = todo.text;
itemall.appendChild(item);
const checkbutton = document.createElement("button");
checkbutton.innerHTML = '<i class="fa-solid fa-check"></i>';
checkbutton.classList.add("check-button");
itemall.appendChild(checkbutton);
const trashbutton = document.createElement("button");
trashbutton.innerHTML = '<i class="fa-solid fa-trash"></i>';
trashbutton.classList.add("trash-button");
itemall.appendChild(trashbutton);
listtdl.appendChild(itemall);
}
// Function to save todo list to localStorage
function saveTodoList(todo) {
const todos = getTodosFromStorage();
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos));
}
// Function to load todos from localStorage
function loadTodoLists() {
const todos = getTodosFromStorage();
todos.forEach(todo => {
createTodoElement(todo);
});
}
// Function to retrieve todos from localStorage
function getTodosFromStorage() {
return JSON.parse(localStorage.getItem('todos')) || [];
}
// Function to handle checking and deleting todo items
function okdel(e) {
const item = e.target;
// Check functionality
if (item.classList[0] === 'check-button') {
const todolist = item.parentElement;
todolist.classList.toggle('checklist');
}
// Delete functionality
if (item.classList[0] === 'trash-button') {
const todolist = item.parentElement;
const todoId = todolist.getAttribute('data-id');
todolist.remove();
removeTodoFromStorage(todoId);
}
}
// Function to remove todo list from local storage
function removeTodoFromStorage(todoId) {
const todos = getTodosFromStorage();
const updatedTodos = todos.filter(todo => todo.id != todoId);
localStorage.setItem('todos', JSON.stringify(updatedTodos));
}