-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript.js
More file actions
86 lines (73 loc) · 3 KB
/
JavaScript.js
File metadata and controls
86 lines (73 loc) · 3 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Array to store cart items in
const cartData = [];
document.addEventListener('DOMContentLoaded', () => {
// Select all "Add to Cart" buttons
const addToCartButtons = document.querySelectorAll('#addToCartBtn');
// Add event listeners to each button
addToCartButtons.forEach((button, index) => {
button.addEventListener('click', () => {
addToCart(index);
});
});
});
function addToCart(index) {
// Product details can be defined here or extracted dynamically
const products = [
{ name: 'T-shirt with tape details', price: 300.00 },
{ name: 'Blue Jeans color blue', price: 900.00 },
{ name: 'Red checks with linings', price: 699.00 },
{ name: 'black t T-shirt', price: 200.00 },
{ name: 'Green checks with black linings', price: 450.00 },
{ name: 'blue short jeans', price: 1050.00 },
{ name: 'T-shirt with color orange', price: 150.00 },
{ name: 'Black Jeans details', price: 899.00 }
];
// Get product details
const product = products[index];
const existingProduct = cartData.find(item => item.name === product.name);
// Increase quantity if the item is already in cart, else add new item
if (existingProduct) {
existingProduct.quantity += 1;
} else {
cartData.push({ ...product, quantity: 1 });
}
// Render cart and update status message
renderCartItems();
document.getElementById('cartStatus').textContent = 'Added to cart!';
}
function renderCartItems() {
const cartItemsContainer = document.getElementById('cart-items');
cartItemsContainer.innerHTML = ''; // Clear existing items
if (cartData.length === 0) {
document.getElementById('empty-cart').style.display = 'block';
document.getElementById('checkout-btn').style.display = 'none';
} else {
document.getElementById('empty-cart').style.display = 'none';
document.getElementById('checkout-btn').style.display = 'block';
cartData.forEach((item, index) => {
const cartItemElement = document.createElement('div');
cartItemElement.classList.add('cart-item');
cartItemElement.innerHTML = `
<div>
<h3>${item.name}</h3>
<p>Price: $${item.price} x ${item.quantity}</p>
</div>
<button onclick="removeItem(${index})">Remove</button>
`;
cartItemsContainer.appendChild(cartItemElement);
});
updateTotalPrice();
}
}
function updateTotalPrice() {
const total = cartData.reduce((sum, item) => sum + (item.price * item.quantity), 0);
document.getElementById('total-price').textContent = total.toFixed(2);
}
function removeItem(index) {
cartData.splice(index, 1); // Remove item from the cart array
renderCartItems(); // Re-render the cart
}
document.getElementById('checkout-btn').addEventListener('click', () => {
alert('Proceeding to checkout...');
// Your checkout logic here
});