-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (55 loc) · 2.09 KB
/
index.js
File metadata and controls
62 lines (55 loc) · 2.09 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
document.addEventListener('DOMContentLoaded', function () {
const products = [
{ id: 1, name: 'shoes', price: 10.99 },
{ id: 2, name: 'bags', price: 19.99 },
{ id: 3, name: 'clothes', price: 7.49 }
];
const cart = [];
const productsSection = document.querySelector('.products');
const cartItems = document.getElementById('cart-items');
const cartTotal = document.getElementById('cart-total');
const checkoutBtn = document.getElementById('checkout-btn');
// Display products
products.forEach(product => {
const productCard = document.createElement('div');
productCard.classList.add('product-card');
productCard.innerHTML = `
<h3>${product.name}</h3>
<p>$${product.price.toFixed(2)}</p>
<button class="add-to-cart-btn" data-id="${product.id}">Add to Cart</button>
`;
productsSection.appendChild(productCard);
});
// Add event listener to 'Add to Cart' buttons
const addToCartButtons = document.querySelectorAll('.add-to-cart-btn');
addToCartButtons.forEach(button => {
button.addEventListener('click', addToCart);
});
// Add to cart function
function addToCart(event) {
const productId = parseInt(event.target.getAttribute('data-id'));
const product = products.find(p => p.id === productId);
if (product) {
cart.push(product);
updateCart();
}
}
// Update cart display
function updateCart() {
cartItems.innerHTML = '';
let total = 0;
cart.forEach(item => {
const li = document.createElement('li');
li.innerHTML = `${item.name} - $${item.price.toFixed(2)}`;
cartItems.appendChild(li);
total += item.price;
});
cartTotal.textContent = `$${total.toFixed(2)}`;
}
// Checkout button
checkoutBtn.addEventListener('click', () => {
alert('Thank you for your purchase!');
cart.length = 0;
updateCart();
});
});