Skip to content

Commit

Permalink
Update menu.js - Streamline add cart process (#16)
Browse files Browse the repository at this point in the history
Modified the existing code to store the saved items in an array to
streamline the process @Anjaliavv51
#5
  • Loading branch information
Anjaliavv51 authored Oct 1, 2024
2 parents 43bb258 + 7728626 commit 9b623da
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit 9b623da

Please sign in to comment.