Shopping Cart Function Developer

Job ID: 40231810

Budget: ₹750 – ₹1,250 INR

let cart = [];
let total = 0;

document.querySelectorAll('.add-to-cart').forEach(button => {
button.addEventListener('click', () => {
const name = button.getAttribute('data-name');
const price = parseFloat(button.getAttribute('data-price'));
cart.push({ name, price });
total += price;
updateCart();
});
});

function updateCart() {
document.getElementById('cart-btn').textContent = `Cart (${cart.length})`;
const cartItems = document.getElementById('cart-items');
cartItems.innerHTML = '';
cart.forEach(item => {
const li = document.createElement('li');
li.textContent = `${item.name} - $${item.price}`;
cartItems.appendChild(li);
});
document.getElementById('total').textContent = total.toFixed(2);
}

document.getElementById('cart-btn').addEventListener('click', () => {
document.getElementById('cart-modal').style.display = 'block';
});

document.querySelector('.close').addEventListener('click', () => {
document.getElementById('cart-modal').style.display = 'none';
});

document.getElementById('checkout-btn').addEventListener('click', () => {
alert('Checkout not implemented yet! This is a demo.');
});

function scrollToToys() {
document.getElementById('toys').scrollIntoView({ behavior: 'smooth' });
}