forked from Fresh-Teacher/NPJS-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (39 loc) · 1.65 KB
/
script.js
File metadata and controls
46 lines (39 loc) · 1.65 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
const btns = document.querySelectorAll(".btn");
const storeProducts = document.querySelectorAll(".store-product");
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function(e) {
const current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
// Switch Tab content
const filter = e.target.dataset.filter;
// console.log(filter);
storeProducts.forEach((product) => {
if (filter === "all") {
product.style.display = "block";
} else if (product.classList.contains(filter)) {
product.style.display = "block";
} else {
product.style.display = "none";
}
});
});
}
// SEARCH FILTER
const search = document.getElementById("search");
const productName = document.querySelectorAll(".product-details h2");
const noResult = document.querySelector(".no-result");
search.addEventListener("keyup", filterProducts);
function filterProducts(e) {
const text = e.target.value.toLowerCase();
productName.forEach((product) => {
const item = product.textContent;
if (item.toLowerCase().indexOf(text) != -1) {
product.parentElement.parentElement.style.display = "block";
noResult.style.display = "none";
} else {
product.parentElement.parentElement.style.display = "none";
noResult.style.display = "block";
}
});
}