?
Solved

JQuery Addition

Posted on 2014-10-01
12
Medium Priority
?
158 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
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

764 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