Welcome to My Online Store
Product 1
Price: $10
Product 2
Price: $20
Product 3
Price: $30
Product 4
Price: $40
Product 5
Price: $50
Product 6
Price: $60
Product 7
$20.99
function addToCart(item) {
// Get the item details
var itemName = item.getAttribute(“data-name”);
var itemPrice = parseFloat(item.getAttribute(“data-price”));
// Create a new cart item object
var cartItem = {
name: itemName,
price: itemPrice,
quantity: 1
};
// Check if the item already exists in the cart
for (var i = 0; i < cart.length; i++) {
if (cart[i].name === itemName) {
cart[i].quantity++;
saveCart();
return;
}
}
// If the item is not already in the cart, add it
cart.push(cartItem);
saveCart();
}
// Update the total price
function updateTotal() {
var total = 0;
for (var i = 0; i < cart.length; i++) {
total += cart[i].price * cart[i].quantity;
}
document.getElementById("total-price").innerHTML = total.toFixed(2);
}
function addToCart(item) {
// Get the item details
var itemName = item.getAttribute("data-name");
var itemPrice = parseFloat(item.getAttribute("data-price"));
// Create a new cart item object
var cartItem = {
name: itemName,
price: itemPrice,
quantity: 1
};
// Check if the item already exists in the cart
for (var i = 0; i < cart.length; i++) {
if (cart[i].name === itemName) {
cart[i].quantity++;
saveCart();
return;
}
}
// If the item is not already in the cart, add it
cart.push(cartItem);
saveCart();
updateTotal();
}
// Update the total price
function updateTotal() {
var total = 0;
for (var i = 0; i < cart.length; i++) {
total += cart[i].price * cart[i].quantity;
}
document.getElementById("total-price").innerHTML = total.toFixed(2);
}
// Save the cart to localStorage
function saveCart() {
localStorage.setItem("cart", JSON.stringify(cart));
}