Jump to content

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.