I am creating an E-Commerce website. Within this E-Commerce website, I have a products page. This products page contains multiple types of products.
I have created seperate buttons that the user can click on to show different products. For example, when the user clicks on the button titled 'Xbox', only products with the platform 'xbox' should show up. The color of the button titled 'Xbox' should also turn red, and any other buttons should turn white.
I have written the following code:
<div id="buttons">
<button class="button-value" onclick="filterProduct('all')">All</button>
<button class="button-value" onclick="filterProduct('xbox')">Xbox</button>
<button class="button-value" onclick="filterProduct('playstation-5')">PlayStation 5</button>
</div>
<div id="products"></div>
#buttons {
text-align: center;
}
.button-value {
border: 2px solid #ff0000;
padding: 1em 2.2em;
border-radius: 3em;
background-color: transparent;
color: #ff0000;
cursor: pointer;
}
.active {
background-color: #ff3333;
color: #ffffff;
}
let products = {
data: [
{
productName: "Alan Wake Remasterd",
developer: "Epic Games",
genre: "",
pegi: "16",
platforms: ["Xbox Series X"],
price: "24.99",
image: "images/images/xbox/alan-wake-remastered-xbox.jpg",
},
{
productName: "Battlefield 2042",
developer: "Electronic Arts",
genre: "",
pegi: "16",
platforms: ["Xbox Series X", "PlayStation 5"],
price: "24.99",
image: "images/images/xbox/battlefield-2042-xbox.jpg",
},
],
};
for (let i of products.data) {
// Create Card
let card = document.createElement("div");
// Card should have a category and should stay hidden initialy
card.classList.add("card", i.category, "hide");
// image div
let imgContainer = document.createElement("div");
imgContainer.classList.add("image-container");
// img tag
let image = document.createElement("img");
image.setAttribute("src", i.image);
imgContainer.appendChild(image);
card.appendChild(imgContainer);
// 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);
// Product Price
let price = document.createElement("h6");
price.innerText = "$" + i.price;
container.appendChild(price);
card.appendChild(container);
document.getElementById("products").appendChild(card);
}
// Parameter passed from button (Parameter same as category)
function filterProduct(value) {
// Button Class Code
let buttons = document.querySelectorAll(".button-value");
buttons.forEach((button) => {
// Check if value equals innerText
if (value.toUpperCase() == button.innerText.toUpperCase()) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
// Select all elements
let elements = document.querySelectorAll(".card");
// Loop through the elements
elements.forEach((element) => {
// Display all cards on all button click
if (value == "all") {
element.classList.remove("hide");
} else {
// Check if element contains category class
if (element.classList.contains(value)) {
// Display elements based on category
element.classList.remove("hide");
} else {
// Hide other elements
element.classList.add("hide");
}
}
});
}
function filterProduct(value) {
// Button Class Code
let buttons = document.querySelectorAll(".button-value");
buttons.forEach((button) => {
// Check if value equals innerText
if (value.toUpperCase() == button.innerText.toUpperCase()) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
This code produces the following output: Output of my HTML, CSS and JavaScript code
However, when I click on the button titled 'Xbox', the color of the button changes, but no cards are displayed. When I click on the button titled 'Playstation 5', the color of the button does not change and the cards are not displayed.
I want the color of the 'Playstation 5' button to also display, and for all of the cards which need to be displayed, to display.