-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
96 lines (79 loc) · 3.72 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
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
87
88
89
90
91
92
93
94
95
96
// Get all the "Add to Cart" buttons
const addButtons = document.querySelectorAll(".add-to-cart");
// Get the cart table body and the cart total element
const cartBody = document.querySelector("#cart-body");
const cartTotal = document.querySelector("#cart-total");
// Initialize the cart items array and the cart total price
let cartItems = [];
let totalPrice = 0;
// Add a click event listener to each "Add to Cart" button
addButtons.forEach(function(button) {
button.addEventListener("click", function() {
// Get the product name and price from the button's data attributes
const productName = button.getAttribute("data-name");
const productPrice = parseFloat(button.getAttribute("data-price"));
// Check if the product is already in the cart
const existingCartItem = cartItems.find(function(item) {
return item.name === productName;
});
if (existingCartItem) {
// If the product is already in the cart, increment the quantity
existingCartItem.quantity++;
existingCartItem.total = existingCartItem.quantity * productPrice;
} else {
// If the product is not in the cart, add it as a new item
const cartItem = {
name: productName,
price: productPrice,
quantity: 1,
total: productPrice,
};
cartItems.push(cartItem);
}
// Update the cart table and the cart total
renderCart();
});
});
// Remove an item from the cart
function removeFromCart(productName) {
cartItems = cartItems.filter(function(item) {
return item.name !== productName;
});
renderCart();
}
// Render the cart table and the cart total
function renderCart() {
// Clear the cart table body
cartBody.innerHTML = "";
// Loop through the cart items and add them to the table body
cartItems.forEach(function(item) {
const row = document.createElement("tr");
const nameCell = document.createElement("td");
nameCell.textContent = item.name;
row.appendChild(nameCell);
const priceCell = document.createElement("td");
priceCell.textContent = "$" + item.price.toFixed(2);
row.appendChild(priceCell);
const quantityCell = document.createElement("td");
quantityCell.textContent = item.quantity;
row.appendChild(quantityCell);
const totalCell = document.createElement("td");
totalCell.textContent = "$" + item.total.toFixed(2);
row.appendChild(totalCell);
const actionCell = document.createElement("td");
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.classList.add("btn", "btn-danger");
removeButton.addEventListener("click", function() {
removeFromCart(item.name);
});
actionCell.appendChild(removeButton);
row.appendChild(actionCell);
cartBody.appendChild(row);
});
// Calculate the cart total and update the cart total element
totalPrice = cartItems.reduce(function(total, item) {
return total + item.total;
}, 0);
cartTotal.textContent = "$" + totalPrice.toFixed(2);
}