-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
65 lines (46 loc) · 1.45 KB
/
Copy pathscripts.js
File metadata and controls
65 lines (46 loc) · 1.45 KB
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
const button = document.querySelector('.button-add-task')
const input = document.querySelector('.input-task')
const listaCompleta = document.querySelector('.list-tasks')
let minhaListaDeItens = []
function adicionarNovaTarefa() {
minhaListaDeItens.push({
tarefa: input.value,
concluida: false,
})
input.value = ''
mostrarTarefas()
}
function mostrarTarefas() {
let novaLi = ''
// ['comprar café', 'estudar programação']
minhaListaDeItens.forEach((item, posicao) => {
novaLi =
novaLi +
`
<li class="task ${item.concluida && 'done'}">
<img src="./img/checked.png" alt="check-na-tarefa" onclick="concluirTarefa(${posicao})">
<p>${item.tarefa}</p>
<img src="./img/trash.png" alt="tarefa-para-o-lixo" onclick="deletarItem(${posicao})">
</li>
`
})
listaCompleta.innerHTML = novaLi
localStorage.setItem('lista', JSON.stringify(minhaListaDeItens))
}
function concluirTarefa(posicao) {
minhaListaDeItens[posicao].concluida = !minhaListaDeItens[posicao].concluida
mostrarTarefas()
}
function deletarItem(posicao) {
minhaListaDeItens.splice(posicao, 1)
mostrarTarefas()
}
function recarregarTarefas() {
const tarefasDoLocalStorage = localStorage.getItem('lista')
if (tarefasDoLocalStorage) {
minhaListaDeItens = JSON.parse(tarefasDoLocalStorage)
}
mostrarTarefas()
}
recarregarTarefas()
button.addEventListener('click', adicionarNovaTarefa)