-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (44 loc) · 1.28 KB
/
script.js
File metadata and controls
59 lines (44 loc) · 1.28 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
function carregarTarefas() {
const tarefas = JSON.parse(localStorage.getItem("tarefas")) || [];
tarefas.forEach(t => criarTarefa(t.texto, t.concluida));
}
function salvarTarefas() {
const lista = document.querySelectorAll("#lista li");
const tarefas = [];
lista.forEach(li => {
tarefas.push({
texto: li.firstChild.textContent,
concluida: li.classList.contains("concluida")
});
});
localStorage.setItem("tarefas", JSON.stringify(tarefas));
}
function criarTarefa(texto, concluida = false) {
const li = document.createElement("li");
li.textContent = texto;
if (concluida) {
li.classList.add("concluida");
}
li.onclick = () => {
li.classList.toggle("concluida");
salvarTarefas();
};
const botaoRemover = document.createElement("button");
botaoRemover.textContent = "X";
botaoRemover.onclick = (e) => {
e.stopPropagation();
li.remove();
salvarTarefas();
};
li.appendChild(botaoRemover);
document.getElementById("lista").appendChild(li);
}
function adicionarTarefa() {
const input = document.getElementById("inputTarefa");
const texto = input.value;
if (texto === "") return;
criarTarefa(texto);
salvarTarefas();
input.value = "";
}
carregarTarefas();