-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.js
66 lines (55 loc) · 2.21 KB
/
product.js
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
document.addEventListener("DOMContentLoaded", function () {
const products = [
{
id: 1,
name: "Product 1",
category: "electronics",
price: 49.99,
image: "product1.jpg",
description: "Description of Product 1."
},
{
id: 2,
name: "Product 2",
category: "clothing",
price: 39.99,
image: "product2.jpg",
description: "Description of Product 2."
},
// Add more products as needed
];
const productContainer = document.querySelector(".product-list");
// Function to display products
function displayProducts(products) {
productContainer.innerHTML = "";
products.forEach(product => {
const productCard = document.createElement("div");
productCard.classList.add("product");
const productImage = document.createElement("img");
productImage.src = product.image;
productImage.alt = product.name;
const productName = document.createElement("h3");
productName.textContent = product.name;
const productPrice = document.createElement("p");
productPrice.textContent = `$${product.price}`;
const addToCartBtn = document.createElement("a");
addToCartBtn.href = "#";
addToCartBtn.classList.add("btn");
addToCartBtn.textContent = "Add to Cart";
addToCartBtn.addEventListener("click", () => addToCart(product));
productCard.appendChild(productImage);
productCard.appendChild(productName);
productCard.appendChild(productPrice);
productCard.appendChild(addToCartBtn);
productContainer.appendChild(productCard);
});
}
// Function to add product to cart
function addToCart(product) {
// Example function, you can implement actual cart functionality here
console.log(`Added ${product.name} to cart.`);
// Update cart popup or functionality as per your design
}
displayProducts(products);
// Additional functionalities and interactivity can be added based on requirements
});