-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscript.js
50 lines (37 loc) · 1.34 KB
/
script.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
const addToCartButtons = document.querySelectorAll('.add-to-cart');
const cartItemsList = document.getElementById('cart-items');
const totalPriceElement = document.getElementById('total-price');
const placeOrderButton = document.getElementById('place-order');
let cart = [];
let totalPrice = 0;
const addToCart = (event) => {
const itemName = event.target.dataset.name;
const itemPrice = parseFloat(event.target.dataset.price);
// Add item to cart
cart.push({ name: itemName, price: itemPrice });
// Update total price
totalPrice += itemPrice;
// Add item to the cart UI
const cartItem = document.createElement('li');
cartItem.textContent = `${itemName} - $${itemPrice.toFixed(2)}`;
cartItemsList.appendChild(cartItem);
// Update the total price displayed
totalPriceElement.textContent = totalPrice.toFixed(2);
// Enable the "Place Order" button
placeOrderButton.disabled = false;
};
addToCartButtons.forEach((button) => {
button.addEventListener('click', addToCart);
});
const placeOrder = () => {
if (cart.length === 0) return;
alert('Order placed successfully!');
cart = [];
totalPrice = 0;
// Clear cart UI
cartItemsList.innerHTML = '';
totalPriceElement.textContent = '0.00';
// Disable the "Place Order" button again
placeOrderButton.disabled = true;
};
placeOrderButton.addEventListener('click', placeOrder);