-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavas.js
More file actions
74 lines (70 loc) · 2.76 KB
/
Copy pathjavas.js
File metadata and controls
74 lines (70 loc) · 2.76 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
66
67
68
69
70
71
72
73
74
const form = document.getElementById("itemForm");
const input = document.getElementById("itemInput");
const daftar = document.getElementById("daftar");
const errorMessage = document.getElementById("errorMessage");
let items = JSON.parse(localStorage.getItem("daftarTugas")) || [];
items.forEach(function(item) {
const li = document.createElement("li");
li.textContent = item.text;
if (item.selesai) {
li.classList.add("selesai");
}
const tombolHapus = document.createElement("button");
tombolHapus.textContent = "❌";
tombolHapus.classList.add("hapus");
li.appendChild(tombolHapus);
li.addEventListener("click", function(e) {
if (e.target.classList.contains("hapus")) {
// Animasi hilang sebelum remove
li.classList.add("hilang");
setTimeout(() => {
li.remove();
items = items.filter(i => i.text !== item.text);
localStorage.setItem("daftarTugas", JSON.stringify(items));
}, 300); // 300ms sesuai durasi animasi CSS
} else {
li.classList.toggle("selesai");
const data = items.find(i => i.text === item.text);
if (data) data.selesai = li.classList.contains("selesai");
localStorage.setItem("daftarTugas", JSON.stringify(items));
}
});
daftar.append(li);
});
form.addEventListener("submit", function(event) {
event.preventDefault();
const itemForm = input.value.trim();
if (itemForm === "") {
errorMessage.textContent = "Hey isi dulu! Field tidak boleh kosong";
input.classList.add("invalid");
return;
}
errorMessage.textContent = "";
input.classList.remove("invalid");
const li = document.createElement("li");
li.textContent = itemForm;
li.classList.add("tampil");
const tombolHapus = document.createElement("button");
tombolHapus.textContent = "❌";
tombolHapus.classList.add("hapus");
li.appendChild(tombolHapus);
li.addEventListener("click", function(e) {
if (e.target.classList.contains("hapus")) {
li.classList.add("hilang");
setTimeout(() => {
li.remove();
items = items.filter(i => i.text !== itemForm);
localStorage.setItem("daftarTugas", JSON.stringify(items));
}, 300);
} else {
li.classList.toggle("selesai");
const data = items.find(i => i.text === itemForm);
if (data) data.selesai = li.classList.contains("selesai");
localStorage.setItem("daftarTugas", JSON.stringify(items));
}
});
daftar.append(li);
items.push({ text: itemForm, selesai: false });
localStorage.setItem("daftarTugas", JSON.stringify(items));
input.value = "";
});