Solved

Addition in Jquery

Posted on 2014-09-26
5
107 Views
Last Modified: 2014-09-26
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
Comment
Question by:rgranlund
  • 3
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40346501
Assuming they are always integers else use parseFloat

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

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40346519
you've a typo end of line 2, valu); -> value;
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40346527
@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
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40346530
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 500 total points
ID: 40346539
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 39
Polygon I google maps 1 16
How to change pointer icon to hand icon on mouseover 2 20
Show/Hide ASP.NET controls with JQuery best practices 3 21
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now