Solved

JQuery Addition

Posted on 2014-10-01
12
133 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now