Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Javascript Addition

I have the following piece of Javascript that I would like to perform a multiplication and ten addition equation.  The Multiplication is working but not the addition.  When it gets to the addition part it appends the figure to the other return.
<script>
function findSum(){
	var baseOne = document.getElementById('addon-28-custom-price[custom-price]').value;
	var myBox1 = document.getElementById('addon-28-custom-price[my-deductible]').value;	
	var myBox2 = document.getElementById('addon-28-custom-price[bicycle-liability]').value;
	var myBox2a = myBox2 != '' ? myBox2 : 0;
	//var myBox3 = document.getElementById('qty3').value;

	//var myBox3a = myBox3 != '' ? myBox3 : 1;

	var result = document.getElementById('result');	
	var myResult = (baseOne) * (myBox1) + (myBox2a);

	 document.getElementById('result').value = myResult;
}
</script>

Open in new window


So at this line (var myResult = (baseOne) * (myBox1) + (myBox2a);) the answer will print out something like: 482.45.50 instead of adding 482 and 45.50 together.
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

You have to use parse method as given below example.

 var sum = parseInt(my_input1) + parseInt(my_input2);

Like Parsing methods for float and date are available. Please use as per your need.
Avatar of duncanb7
duncanb7

Try to use eval() on javascript  mentioned at this site , http://www.w3schools.com/jsref/jsref_eval.asp

Duncan
Avatar of Robert Granlund

ASKER

is the following correct?
<script>
function findSum(){
	var baseOne = document.getElementById('addon-28-custom-price[custom-price]').value;
	var myBox1 = document.getElementById('addon-28-custom-price[my-deductible]').value;	
	var myBox2 = document.getElementById('addon-28-custom-price[bicycle-liability]').value;
	var myBox2a = myBox2 != '' ? myBox2 : 0;
	
	var myBox3 = document.getElementById('addon-28-custom-price[medical-payments]').value;
	var myBox3a = myBox3 != '' ? myBox3 : 0;

	var result = document.getElementById('result');	
	var prep = (baseOne) * (myBox1);
	var myResult = prep + parseInt(myBox2a);

	 document.getElementById('result').value = myResult;
}
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is the following correct?
We can't tell.  We would need to see the HTML for the form, and we would need to know what data the client typed into the form.
Please provide an explanation of the marked-down grade.  To quote from the E-E grading guidelines, "B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.

What was wrong with the answer?  What were you expecting that you didn't get?