-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
153 lines (134 loc) · 4.19 KB
/
script.js
File metadata and controls
153 lines (134 loc) · 4.19 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let products = {
data : [
{
productName: "Regular white T-shirt",
category: "Topwear",
price: "30",
image: "img/white-tshirt.jpg",
},
{
productName: "Beige Short Skirt",
category: "Bottomwear",
price: "49",
image: "img/short-skirt.jpg",
},
{
productName: "Sporty SmartWatch",
category: "Watch",
price: "99",
image: "img/sporty-smartwatch.jpg",
},
{
productName: "Basic Knitted Top",
category: "Topwear",
price: "29",
image: "img/knitted-top.jpg",
},
{
productName: "Black Leather Jacket",
category: "Jacket",
price: "129",
image: "img/black-leather-jacket.jpg",
},
{
productName: "Stylish Trousers",
category: "Bottomwear",
price: "89",
image: "img/trousers.jpg",
},
{
productName: "Brown Men's Jacket",
category: "Jacket",
price: "189",
image: "img/brown-jacket.jpg",
},
{
productName: "Comfy Gray Pants",
category: "Bottomwear",
price: "49",
image: "img/comfy-gray-pants.jpg",
},
],
};
// Access object data
for (let i of products.data) {
// create card
let card = document.createElement("div");
// card should have category and initially hide
card.classList.add("card", i.category, "hide");
// image div
let imageContainer = document.createElement("div");
imageContainer.classList.add("image-container");
// img tag
let image = document.createElement("img");
image.setAttribute("src", i.image);
imageContainer.appendChild(image);
card.appendChild(imageContainer);
// container
let container = document.createElement("div");
container.classList.add("container");
// product name
let name = document.createElement("h5");
name.classList.add("product-name");
name.innerText = i.productName.toUpperCase();
container.appendChild(name);
// price of products
let price = document.createElement("h3");
price.classList.add("product-price");
price.innerText = "$ " + i.price;
container.appendChild(price);
card.appendChild(container);
document.getElementById("products").appendChild(card);
}
// category
function filterProduct(value) {
// Button class code
let buttons = document.querySelectorAll(".button-value");
console.log(buttons);
buttons.forEach((button) => {
// check if value equals to innerText
if (value.toUpperCase() == button.innerText.toUpperCase()) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
// select all cards
let elements = document.querySelectorAll(".card");
// loop through all cards
elements.forEach((element) => {
// display all card on 'all' button click
if (value == "all") {
element.classList.remove("hide");
} else {
// check if elements contains category
if (element.classList.contains(value)) {
// display element based on category
element.classList.remove("hide");
} else {
// hide other element
element.classList.add("hide");
}
}
});
}
// search button click
document.getElementById("search").addEventListener
("click", () => {
// Initializations
let searchInput = document.getElementById("search-input").value;
let elements = document.querySelectorAll(".product-name");
let cards = document.querySelectorAll(".card");
elements.forEach((element, index) => {
if (element.innerText.includes(searchInput.toUpperCase())) {
// display matching
cards[index].classList.remove("hide");
} else {
cards[index].classList.add("hide");
}
});
});
// initially display all products
window.onload = () => {
filterProduct("all");
};