-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
31 lines (25 loc) · 1 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
//add to cart functionality
document.addEventListener('DOMContentLoaded', function () {
// Initialize the cart from local storage or create an empty cart
let cart = JSON.parse(localStorage.getItem('cart')) || [];
// Function to add items to the cart
window.addToCart = function (productName, price) {
const item = {
name: productName,
price: price
};
// Add the item to the cart
cart.push(item);
// Update the cart in local storage
localStorage.setItem('cart', JSON.stringify(cart));
// Alert the user that the item has been added to the cart
alert(`Added ${productName} to the cart.`);
};
// Function to update the cart count
function updateCartCount() {
const cartCountElement = document.getElementById('cart-count');
cartCountElement.textContent = cart.length;
}
// Call the function to update the initial cart count
updateCartCount();
});