javascript - localStorage issue - Items not displayed (JSON/JQUERY) -
i'm trying , sort items in localstorage , output html page.
this i'm doing:
<script> function shoppingcart() { var totalprice = 0; 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) // check if key startwith 'product_' { var product = localstorage.getitem('product_'+i); var result = json.parse(product); var productname; var productalbum; var productquantity; var productprice; var productsubtotal = 0; var totalprice; productname = result.name productalbum = result.album; productquantity = result.quantity; productprice = parsefloat(result.price).tofixed(2); productsubtotal = parsefloat(productquantity * 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 outputtotal = "<table><tr><td><b>total:</b> eur " + totalprice + "</td></tr></table>"; var totaloutput = outputname + outputalbum + outputquantity + outputprice + outputsubtotal + outputtotal; document.getelementbyid("cart-contents").innerhtml=totaloutput; } } alert(totaloutput); } window.onload = shoppingcart; </script>
the item being output item named 'proudct_0' in localstorage. others not being displayed!
this have in localstorage: http://i.imgur.com/shxxlol.png
any idea why happening ?
something wrong in code. think if product_0 not in localstorage?
var product = localstorage.getitem('product_'+i); var result = json.parse(product);
may null , throw error.
try this:
for (var = 0; < localstorage.length-1; i++){ var keyname = localstorage.key(i); if(keyname.indexof('product_')==0) // check if key startwith 'product_' { var product = localstorage.getitem(keyname); //do code here } }
update
document.getelementbyid("cart-contents").innerhtml=totaloutput;
it's replace, not append
hope help!