javascript - Some localStorage items are not being displayed -
i have 5 items in localstorage, however, when try output files,
only 2 items being output, , not all.
js puts localstorage:
$(document).ready(function () { $.fn.savetocart = function (id, iname, ialbum, iprice) { var quantity = 0; if (localstorage.getitem('product_' + id) === null) { quantity = 1; } else { var tempitem = json.parse(localstorage.getitem('product_' + id)); var tempquantity = parseint(tempitem['quantity']); quantity = tempquantity + 1; } var cartitem = { 'quantity': quantity, 'name': iname, 'album': ialbum, 'price': iprice } localstorage.setitem('product_' + id, json.stringify(cartitem)); };
cart js:
function shoppingcart() { var output; var productname; var productalbum; var productquantity; var productprice; var productsubtotal = 0; var totalprice; (var = 0; < localstorage.length - 1; i++) { var keyname = localstorage.key(i); if (keyname.indexof('product_') == 0) { var product = localstorage.getitem(keyname); var product = localstorage.getitem('product_' + i); var result = json.parse(product); totalprice = 0; productname = result.name productalbum = result.album; productquantity = parseint(result.quantity); productprice = parsefloat(result.price).tofixed(2); productsubtotal = parseint(productquantity) * parsefloat(productprice).tofixed(2); outputname = "<div id='cart-table'><table><tr><td><b>name: </b>" + productname + "</td></tr></div>"; outputalbum = "<tr><td><b>album: </b>" + productalbum + "</td></tr>"; outputquantity = "<tr><td><b>quantity: </b>" + productquantity + "</td></tr>"; outputprice = "<tr><td><b>price: </b> eur " + productprice + "</td></tr>"; outputsubtotal = "<tr><td><b>sub-total: </b> eur " + productsubtotal + "</td></tr></table><br><br>"; var totaloutput = outputname + outputalbum + outputquantity + outputprice + outputsubtotal; document.getelementbyid("cart-contents").innerhtml = document.getelementbyid("cart-contents").innerhtml + totaloutput; totalprice += parsefloat(totalprice) + parsefloat(productsubtotal); productsubtotal = productsubtotal + productsubtotal; } i++; } totalprice = productsubtotal; localstorage.setitem("price", totalprice); var outputtotal = "<br><br><h1><table><tr><td><b>total:</b> eur " + totalprice + "</td></tr></table></h1>"; document.getelementbyid("total-price").innerhtml = document.getelementbyid("total-price").innerhtml + outputtotal; } function clearcart() { localstorage.clear(); document.location.reload(true); } window.onload = shoppingcart; </script>
html:
<div id="cart-contents"></div> <div id="total-price"></div>
also, have problem, total price not getting calculated correctly. getting 28.12 total of 14.06 + 16.40. why happening?
to give idea:
change loop this: for (var = 0; < localstorage.length; i++){
.
and don't add i++;
end of loop's body.
both cause loop not hit items, different reasons.