var cart = []; $(function () { if (localStorage.cart) { // load cart data from local storage cart = JSON.parse(localStorage.cart); showCart(); // display cart that is loaded into cart array } });
function addToCart() { var price = $("#products").val(); // get selected product's price var name = $("#products option:selected").text(); // get product name var qty = $("#qty").val(); // get quantity // update Qty if product is already present for (var i in cart) { if(cart[i].Product == name) { cart[i].Qty = qty; // replace existing Qty showCart(); saveCart(); return; } } var item = { Product: name, Price: price, Qty: qty }; cart.push(item); saveCart(); showCart(); }
function saveCart() { if (window.localStorage) { localStorage.cart = JSON.stringify(cart); } }
function showCart() { if (cart.length == 0) { $("#cart").css("visibility", "hidden"); // hide table that shows cart return; } $("#cart").css("visibility", "visible"); $("#cartBody").empty(); // empty tbody of table for (var i in cart) { var item = cart[i]; var row = "<tr><td>" + item.Product + "</td><td>" + item.Price + "</td><td>" + item.Qty + "</td><td>" + item.Qty * item.Price + "</td><td>" + "<button onclick='deleteItem(" + i + ")'>Delete</button></td></tr>"; $("#cartBody").append(row); } }
<h2>Cart</h2> Select Product : <select id="products"> <option value="50000">iPhone6</option> <option value="60000">iPhone6 Plus</option> <option value="45000">iPad Air 2 with Cellular</option> </select> Quantity : <input type="text" id="qty" size="3" /> <p/> <button id="btnAdd" onclick="addToCart()" >Add To Cart</button> <p /> <table id="cart" border="1" style="visibility:hidden; width:100%"> <thead> <tr> <th>Product</th> <th>Price</th> <th>Qty</th> <th>Total</th> <th></th> </tr> </thead> <tbody id="cartBody"> </tbody> </table>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Shopping Cart</title> <style> td { text-align :center} </style> <script src="jquery-1.11.2.js"></script> <script> var cart = []; $(function () { if (localStorage.cart) { cart = JSON.parse(localStorage.cart); showCart(); } }); function addToCart() { var price = $("#products").val(); var name = $("#products option:selected").text(); var qty = $("#qty").val(); // update qty if product is already present for (var i in cart) { if(cart[i].Product == name) { cart[i].Qty = qty; showCart(); saveCart(); return; } } // create JavaScript Object var item = { Product: name, Price: price, Qty: qty }; cart.push(item); saveCart(); showCart(); } function deleteItem(index){ cart.splice(index,1); // delete item at index showCart(); saveCart(); } function saveCart() { if ( window.localStorage) { localStorage.cart = JSON.stringify(cart); } } function showCart() { if (cart.length == 0) { $("#cart").css("visibility", "hidden"); return; } $("#cart").css("visibility", "visible"); $("#cartBody").empty(); for (var i in cart) { var item = cart[i]; var row = "<tr><td>" + item.Product + "</td><td>" + item.Price + "</td><td>" + item.Qty + "</td><td>" + item.Qty * item.Price + "</td><td>" + "<button onclick='deleteItem(" + i + ")'>Delete</button></td></tr>"; $("#cartBody").append(row); } } </script> </head> <body> <h2>Cart</h2> Select Product : <select id="products"> <option value="50000">iPhone6</option> <option value="60000">iPhone6 Plus</option> <option value="45000">iPad Air 2 with Cellular</option> </select> Quantity <input type="text" id="qty" size="3" /> <p/> <button id="btnAdd" onclick="addToCart()" >Add To Cart</button> <p></p> <table id="cart" border="1" style="visibility:hidden; width:100%"> <thead> <tr> <th>Product</th> <th>Price</th> <th>Qty</th> <th>Total</th> <th></th> </tr> </thead> <tbody id="cartBody"> </tbody> </table> </body> </html>