-
Notifications
You must be signed in to change notification settings - Fork 16
/
script.js
39 lines (32 loc) · 1.11 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
// Load todos from localStorage at startup
document.addEventListener('DOMContentLoaded', function() {
var todos = JSON.parse(localStorage.getItem('todos')) || [];
todos.forEach(addItem);
});
document.getElementById('add-todo').addEventListener('click', function() {
var value = document.getElementById('todo-input').value;
if (value) {
addItem(value);
document.getElementById('todo-input').value = '';
saveTodos();
}
});
function addItem(text) {
var list = document.getElementById('todo-list');
var item = document.createElement('li');
item.innerText = text;
var deleteButton = document.createElement('button');
deleteButton.innerText = 'Delete';
deleteButton.addEventListener('click', function() {
list.removeChild(item);
saveTodos();
});
item.appendChild(deleteButton);
list.appendChild(item);
}
function saveTodos() {
var todos = Array.from(document.getElementById('todo-list').children).map(function(item) {
return item.innerText.replace('Delete', '');
});
localStorage.setItem('todos', JSON.stringify(todos));
}