Solved

JQuery Addition

Posted on 2014-10-01
12
146 Views
Last Modified: 2014-10-01
I have a JQuery addition question:
var roadside = 10;
var baseOne = 100;
var myAss = 200;

var preBase = parseInt(baseOne) + parseInt(myAss) + parseInt(roadside);

Open in new window


preBase should = 310  BUT it is coming out as 301.  The first two (baseOne + myAss) adds up correctly, but the last part does not work correct.
0
Comment
Question by:rgranlund
  • 5
  • 4
  • 3
12 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40355811
Code works fine, this isn't a comma issue again is it?

http://jsfiddle.net/g5zpk206/
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40355813
test page : http://jsfiddle.net/zwu8jzsz/

var roadside = 10;
var baseOne = 100;
var myAss = 200;

var preBase = baseOne * 1 + myAss * 1 + roadside * 1;

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 40355896
I forgot to include part of the code:
var roadside = 10;
var baseOne = 100;
var myAss = 200;


var roadside = 10.00;
var baseOne = document.getElementById('custom-price').value;
var myBox10 = document.getElementById('valueOfAccessories').value;
	if(myBox10 != '') {
		myAss = myBox10
	} else {
		myAss = 0
	}

var preBase = parseInt(baseOne.replace(",","")) + parseInt(myAss) + parseInt(roadside);

Open in new window

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 82

Expert Comment

by:leakim971
ID: 40355908
var roadside = 10.00;
var baseOne = document.getElementById('custom-price').value * 1 || 0;
var myBox10 = document.getElementById('valueOfAccessories').value * 1 || 0;
var preBase = baseOne + myBox10 + roadside;

alert( preBase );
0
 
LVL 58

Expert Comment

by:Gary
ID: 40355909
Refer your previous question - you probably have a comma in that value

http://www.experts-exchange.com/Q_28526435.html
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40355918
Test page : http://jsfiddle.net/zwu8jzsz/2/
It's wrong to do : .replace(",","")
use this instead : .replace(",",".")

window.onload = function() {
    document.getElementById('custom-price').onkeyup = calc;
    document.getElementById('valueOfAccessories').onkeyup = calc;
};

function calc() {
    var roadside = 10.00;
    var baseOne = document.getElementById('custom-price').value.replace(",",".") * 1 || 0;
    var myBox10 = document.getElementById('valueOfAccessories').value.replace(",",".") * 1 || 0;
    var preBase = baseOne + myBox10 + roadside;
    
    document.getElementById('preBase').value = preBase;
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40355921
But that will turn 1,000 into 1
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40355930
you right so what is "," ? a comma or the thoursand separator?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40355934
I have no commas in the roadside var
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40355939
what did you exactly put in the two textbox ?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40355946
Alert the values of those two boxes and post the unedited results here.
0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 40356089
Please forgive me, it was a comma.  However, thank you for sticking with me and offering your help!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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)

839 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