Building a Simple Shopping Cart with JavaScript & LocalStorage

🚀 Just Built a Simple Shopping Cart using JavaScript & LocalStorage! I practiced JavaScript DOM manipulation and LocalStorage by building a small cart system where users can add products with quantities and keep the data saved even after refreshing the page. 💡 Features: • Add product with quantity • Update quantity if product already exists • Store data in LocalStorage • Retrieve stored cart after page reload 🧑💻 Sample Code: const handleAddCart = () => { const productEl = document.getElementById('product') const quantityEl = document.getElementById('quantity') const product = productEl.value; const quantity = parseInt(quantityEl.value); productAddToUl(product, quantity); addToCart(product, quantity) productEl.value = ''; quantityEl.value = ''; } const getCart = () => { let cart = {} const cartJson = localStorage.getItem('cart'); if(cartJson){ cart = JSON.parse(cartJson) } return cart; } const addToCart = (product, quantity) => { const cart = getCart(); if(cart[product]){ cart[product] = cart[product] + quantity; }else{ cart[product] = quantity; } const cartJson = JSON.stringify(cart); localStorage.setItem('cart', cartJson) } #JavaScript #WebDevelopment #FrontendDeveloper #LocalStorage #Coding GitHub Repository:https://lnkd.in/gYrfSkkY

To view or add a comment, sign in

Explore content categories