-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
52 lines (43 loc) · 1.76 KB
/
main.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
let cart = []; // Array to hold cart items
function addToCart(product) {
// Add the product to the cart array
cart.push(product);
// Display a message that the item has been added to the cart
const messageBox = document.getElementById('fardeen');
const messageText = document.getElementById('waqas');
messageText.textContent = product + ' has been added to your cart!';
messageBox.style.display = 'block';
// Hide the message after 3 seconds
setTimeout(function() {
messageBox.style.display = 'none';
}, 3000);
// Update the cart display
updateCart();
}
function updateCart() {
const cartItemsContainer = document.getElementById('cartItems');
cartItemsContainer.innerHTML = ''; // Clear previous cart items
if (cart.length === 0) {
cartItemsContainer.innerHTML = '<p>Your cart is empty.</p>';
document.getElementById('checkoutButton').style.display = 'none'; // Hide checkout button if the cart is empty
} else {
cart.forEach((item, index) => {
const cartItem = document.createElement('div');
cartItem.classList.add('cart-item');
cartItem.innerHTML = `
<p>${item}</p>
<button class="button" onclick="removeFromCart(${index})">Remove</button>
`;
cartItemsContainer.appendChild(cartItem);
});
document.getElementById('checkoutButton').style.display = 'block'; // Show checkout button
}
}
function removeFromCart(index) {
cart.splice(index, 1); // Remove the item at the given index
updateCart(); // Update the cart display
}
function checkout() {
alert('Proceeding to checkout with items: ' + cart.join(', '));
// Redirect to checkout page or handle checkout logic
}