Solved

Addition in Jquery

Posted on 2014-09-26
5
111 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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)

733 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