-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (43 loc) · 1.12 KB
/
app.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
const typer = document.querySelector('.typer')
const taskList = document.querySelector('.tasks')
const debug = document.querySelector('span')
alert('Executou javascript')
getTasksFromSave()
typer.addEventListener('keyup', event => {
debug.innerText = event.code
if (event.code == 'Enter' || event.code == 'NumpadEnter') {
add(event.target.value)
cleanTyper()
}
})
function getTasksFromSave() {
const tasks = JSON.parse(localStorage.getItem('tasks') || '[]')
tasks.forEach(task => add(task))
}
function cleanTyper() {
typer.value = ''
}
function add(task) {
const li = document.createElement('li')
li.classList.add('task')
li.innerHTML = `
${task}
<img class="delete-icon" src="./trash-can-solid.svg" alt="Delete icon" onclick="deleteTask(this.parentElement)">
`
taskList.appendChild(li)
save()
}
function deleteTask(task) {
task.classList.add('removing')
setTimeout(() => {
task.remove()
save()
}, 500)
}
function save() {
let tasks = []
taskList.querySelectorAll('.task').forEach(task => {
tasks.push(task.innerText)
})
localStorage.setItem('tasks', JSON.stringify(tasks))
}