From 7728626f23e22163c2e2800bce9b2d485e3777ff Mon Sep 17 00:00:00 2001 From: Rhea <88110289+rees8@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:31:37 +0530 Subject: [PATCH] Update menu.js - Streamline add cart process Modified the existing code to store the saved items in an array to streamline the process. --- menu.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/menu.js b/menu.js index 25fde170..f8c1497a 100644 --- a/menu.js +++ b/menu.js @@ -8,9 +8,16 @@ document.addEventListener('DOMContentLoaded', function () { var itemName = item.querySelector('h3').textContent; var itemPrice = item.querySelector('p').textContent; alert("Item added to cart successfully"); + + // Retrieve existing cart items from localStorage + var cartItems = JSON.parse(localStorage.getItem('cartItems')) || []; + + // Add new item to the cart array + var newItem = { name: itemName, price: itemPrice }; + cartItems.push(newItem); - localStorage.setItem('itemName', itemName); - localStorage.setItem('itemPrice', itemPrice); + // Save updated cart back to localStorage + localStorage.setItem('cartItems', JSON.stringify(cartItems)); window.location.href = "cart.html"; }