-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (48 loc) · 1.63 KB
/
script.js
File metadata and controls
54 lines (48 loc) · 1.63 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
const products = [
{ id: 1, name: "Sneakers", price: 1999, img: "https://via.placeholder.com/250x200?text=Sneakers" },
{ id: 2, name: "Headphones", price: 2499, img: "https://via.placeholder.com/250x200?text=Headphones" },
{ id: 3, name: "Smart Watch", price: 3499, img: "https://via.placeholder.com/250x200?text=Smart+Watch" },
{ id: 4, name: "Backpack", price: 1299, img: "https://via.placeholder.com/250x200?text=Backpack" },
{ id: 5, name: "Sunglasses", price: 899, img: "https://via.placeholder.com/250x200?text=Sunglasses" }
];
let cart = [];
// Render products
const productContainer = document.getElementById("products");
products.forEach(p => {
productContainer.innerHTML += `
<div class="card">
<img src="${p.img}" alt="${p.name}">
<h3>${p.name}</h3>
<p class="price">₹${p.price}</p>
<button class="btn" onclick="addToCart(${p.id})">Add to Cart</button>
</div>
`;
});
function toggleCart() {
document.getElementById("cart").classList.toggle("active");
}
function addToCart(id) {
const product = products.find(p => p.id === id);
cart.push(product);
document.getElementById("cart-count").innerText = cart.length;
renderCart();
}
function renderCart() {
const cartItems = document.getElementById("cart-items");
cartItems.innerHTML = "";
cart.forEach((item) => {
cartItems.innerHTML += `
<div class="cart-item">
<span>${item.name}</span>
<span>₹${item.price}</span>
</div>
`;
});
}
function checkout() {
if(cart.length === 0) {
alert("Your cart is empty!");
} else {
alert("Proceeding to checkout with " + cart.length + " items.");
}
}