Solved

JQuery Addition

Posted on 2014-10-01
12
153 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 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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…

691 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