Jump to content

javascript cart issue


Isset1988

Recommended Posts

Hello :)

I have two issues with a javascript cart.

  1. 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.
  2. 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! :D

Link to comment
https://forums.phpfreaks.com/topic/278773-javascript-cart-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.