?
Solved

Addition in Jquery

Posted on 2014-09-26
5
Medium Priority
?
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

801 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