Solved

JQuery Addition

Posted on 2014-10-01
12
144 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Title # Comments Views Activity
Wordpress Taxonomy 2 28
Jquery GMAP3 Hierarchy 1 27
Why doesn't Google directions work? 6 46
jquery datable hidden section doesn't submit 2 9
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.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)

773 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