-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
48 lines (37 loc) · 1.26 KB
/
main.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
const taskInput = document.getElementById('do');
const addButton = document.getElementById('addTask');
const taskList = document.getElementById('taskList');
loadTask();
function addTask() {
let task = taskInput.value.trim();
if (task) {
createTaskElement(task);
saveTask();
taskInput.value = '';
}
}
function createTaskElement(task) {
let listItem = document.createElement('li');
listItem.textContent = task;
let deleteButton = document.createElement('button');
deleteButton.innerHTML = '<i class="fa-solid fa-trash"></i>';
deleteButton.className = 'deleteTask';
listItem.appendChild(deleteButton);
taskList.appendChild(listItem);
deleteButton.addEventListener('click', function(){
taskList.removeChild(listItem);
saveTask();
})
}
addButton.addEventListener('click', addTask);
function saveTask() {
let tasks = [];
taskList.querySelectorAll('li').forEach(function (item) {
tasks.push(item.textContent.trim());
});
localStorage.setItem('tasks', JSON.stringify(tasks));
}
function loadTask() {
const tasks = JSON.parse(localStorage.getItem('tasks') || '[]');
tasks.forEach(task => createTaskElement(task));
}