-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (74 loc) · 2.16 KB
/
Copy pathscript.js
File metadata and controls
101 lines (74 loc) · 2.16 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const form = document.getElementById("item-form");
const itemInput = document.getElementById("item-input");
const itemList = document.getElementById("item-list");
const filter = document.getElementById("filter");
const clear = document.getElementById("clear");
function addItem(event) {
event.preventDefault();
let newItem = itemInput.value;
// Check if the input is empty
if (newItem.trim().length === 0) {
itemInput.classList.add("error");
alert("Please enter an item.");
return;
}
const li = document.createElement("li");
li.appendChild(document.createTextNode(newItem));
const btn = createBtn("remove-item btn-link text-red");
li.appendChild(btn);
itemList.appendChild(li);
checkUI();
itemInput.value = "";
}
function createBtn(classes) {
const btn = document.createElement("button");
btn.className = classes;
const icon = createIcon("fa-solid fa-xmark");
btn.appendChild(icon);
return btn;
}
function createIcon(classes) {
const icon = document.createElement("i");
icon.className = classes;
return icon;
}
function removeItem(event) {
if (event.target.parentElement.classList.contains("remove-item")) {
event.target.parentElement.parentElement.remove();
}
checkUI();
}
function removeAllItems() {
while (itemList.firstChild) {
//apple will removed
itemList.removeChild(itemList.firstChild);
}
checkUI();
}
function checkUI() {
const items = itemList.querySelectorAll("li");
if (items.length === 0) {
filter.style.display = "none";
clear.style.display = "none";
} else {
filter.style.display = "block";
clear.style.display = "block";
}
}
function filterItem() {
const items = itemList.querySelectorAll("li");
const text = event.target.value;
items.forEach((item) => {
const itemName = item.firstChild.textContent.toLowerCase().trim();
if (itemName.indexOf(text) != -1) {
item.style.display = 'flex'
}else {
item.style.display = 'none'
}
})
}
checkUI();
form.addEventListener("submit", addItem);
itemList.addEventListener("click", removeItem);
clear.addEventListener("click", removeAllItems);
filter.addEventListener("input", filterItem);