• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 121
  • Last Modified:

Addition in Jquery

I am trying to do addition in JQuery but I'm not sure what I am doing:
baseOne = 1000;
baseTwo = 300
	var baseOne = document.getElementById('custom-price').value;  
        var baseTwo= document.getElementById('valueOfAccessories').valu);

        var preBase = baseOne + baseTwo;

Open in new window

For preBAse I get 1000300 instead of 1300.  What am I doing wrong?
0
rgranlund
Asked:
rgranlund
  • 3
2 Solutions
 
GaryCommented:
Assuming they are always integers else use parseFloat

var preBase = parseInt(baseOne) + parseInt(baseTwo);

Open in new window

0
 
leakim971PluritechnicianCommented:
you've a typo end of line 2, valu); -> value;
0
 
rgranlundAuthor Commented:
@Gary, when I do that it gives me 301.  It took the first three zeros off on 1000.
baseOne = 1000;
baseTwo = 300
var baseOne = document.getElementById('custom-price').value;  
 var baseTwo = document.getElementById('valueOfAccessories').value;

        var preBase = parseInt(baseOne) + parseInt(baseTwo);

Open in new window

0
 
GaryCommented:
Shouldn't do, are you sure it is 1000 and not 1,000 which would give you 301

Try
var preBase = parseInt(baseOne.replace(",","")) + parseInt(baseTwo);

Open in new window

0
 
GaryCommented:
p.s.
If you are using jQuery why are you using vanilla js to get the values instead of...

var baseOne = $("#custom-price").val();  
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now