-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
63 lines (47 loc) · 1.37 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
const form = document.getElementById('form')
const input = document.getElementById('input')
const todosUL = document.getElementById('todos')
const todos = JSON.parse(localStorage.getItem('todos'))
if(todos) {
todos.forEach(todo => addTodo(todo))
}
form.addEventListener('submit', (e) => {
e.preventDefault()
addTodo()
})
function addTodo(todo) {
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.innerHTML = todoText
todoEL.addEventListener('click', () => {
todoEL.classList.toggle('completed')
updateLS
})
todoEL.addEventListener('contextmenu', (e) => {
e.preventDefault()
todoEL.remove()
updateLS
})
todosUL.appendChild(todoEL)
input.value = ''
updateLS()
}
}
function updateLS() {
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))
}