-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (48 loc) · 1.58 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
document.addEventListener('DOMContentLoaded', loadTasks);
document.getElementById('addTaskBtn').addEventListener('click', addTask);
function addTask() {
const taskInput = document.getElementById('taskInput');
const taskText = taskInput.value.trim();
if (taskText) {
const tasks = getTasks();
tasks.push({ text: taskText, completed: false });
localStorage.setItem('tasks', JSON.stringify(tasks));
taskInput.value = '';
renderTasks();
}
}
function getTasks() {
return JSON.parse(localStorage.getItem('tasks')) || [];
}
function loadTasks() {
renderTasks();
}
function renderTasks() {
const taskList = document.getElementById('taskList');
taskList.innerHTML = '';
const tasks = getTasks();
tasks.forEach((task, index) => {
const taskItem = document.createElement('li');
taskItem.className = 'task-item' + (task.completed ? ' completed' : '');
taskItem.innerHTML = `
<span>${task.text}</span>
<div>
<button onclick="toggleComplete(${index})">✔️</button>
<button onclick="deleteTask(${index})">❌</button>
</div>
`;
taskList.appendChild(taskItem);
});
}
function toggleComplete(index) {
const tasks = getTasks();
tasks[index].completed = !tasks[index].completed;
localStorage.setItem('tasks', JSON.stringify(tasks));
renderTasks();
}
function deleteTask(index) {
const tasks = getTasks();
tasks.splice(index, 1);
localStorage.setItem('tasks', JSON.stringify(tasks));
renderTasks();
}