Isset1988 Posted June 4, 2013 Share Posted June 4, 2013 Hello I have two issues with a javascript cart. When i change the quantities, i see updated the total and the subtotal, but ... when i load the page, the total and subtotal is empty by default. Can i print above the subtotal and the total-quantity? Here i attach you my code: <script> var books = {} window.onload=function() { var inputs = document.getElementsByTagName('input'); for (var i=0;i<inputs.length;i++) { if (inputs[i].type=="text" && inputs[i].id.indexOf('quantity')!=-1) { var name = inputs[i].id.replace('quantity',''); books[name] = parseFloat(document.getElementById(name+'price').innerHTML.replace('$','')) inputs[i].onkeyup=function() { var total = 0; for (var book in books) { var q = document.getElementById(book+"quantity").value; total += (isNaN(q) || q=="")?0:parseInt(q)*books[book] } document.getElementById('subtotal').innerHTML="Sub-total: "+total.toFixed(2)+" €"; document.getElementById('grandtotal').innerHTML="Total: "+(total*1.23).toFixed(2)+" €"; } } } } </script> <p id="1price">100 €</p> <input type="text" id="1quantity" value='1' /> <p id="2price">100 €</p> <input type="text" id="2quantity" value='1' /> <div class="total-subtotal"> <p class="totals" id="subtotal">Sub-total:</p> </div> <div class="total-price"> <p class="totals" id="grandtotal">Grand-total:</p> </div> Is there someone who can help? Thank U! Link to comment https://forums.phpfreaks.com/topic/278773-javascript-cart-issue/ Share on other sites More sharing options...
Isset1988 Posted June 5, 2013 Author Share Posted June 5, 2013 Any idea? Is there any other way to update the quantities and the total price using javascript? Link to comment https://forums.phpfreaks.com/topic/278773-javascript-cart-issue/#findComment-1434196 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.