-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.js
109 lines (49 loc) · 1.54 KB
/
super.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
102
103
104
105
106
107
108
109
const form = document.getElementById('form');
const input = document.getElementById('input');
const todosUl = document.getElementById('todos');
// Load todos from local storage
const todos = JSON.parse(localStorage.getItem('todos'));
if (todos) {
todos.forEach(todo => addTodo(todo));
}
form.addEventListener('submit', (e) => {
e.preventDefault();
addTodo();
});
function addTodo(todo = null) {
let todoText = input.value;
if (todo) {
todoText = todo.text;
}
if (todoText) {
const todoEl = document.createElement('li');
if (todo && todo.completed) {
todoEl.classList.add('completed');
}
todoEl.innerText = todoText;
todoEl.addEventListener('click', () => {
todoEl.classList.toggle('completed');
updateLS();
});
todoEl.addEventListener('contextmenu', (e) => {
e.preventDefault();
todoEl.remove();
updateLS();
});
todosUl.appendChild(todoEl);
input.value = '';
// Update local storage
updateLS();
}
}
function updateLS() {
const todosEl = document.querySelectorAll('li');
const todos = [];
todosEl.forEach((todoEl) => {
todos.push({
text: todoEl.innerText,
completed: todoEl.classList.contains('completed')
});
});
localStorage.setItem('todos', JSON.stringify(todos));
}