JCD JCD - 10 months ago 82
jQuery Question

Simple math but its not working?

I have a table where users can enter a part number, part price and quantity. It then multiple's the price and the quantity to get you a total. I got this part up and running.

Now i have to get the taxes on the part and then a grand total but this is what im getting:

function calculateit() {
var myBox1 = $( 'input[name=tax2]:checked' ).val(); //taxes value
var myBox2 = document.getElementById('partstotalvalue').value; //parts total value
var result = document.getElementById('partstax'); // input field for the total of taxes * parts
var myResult = myBox1 * myBox2; //result = taxes * parts total
result.value = myResult; // display the results

var result2 = document.getElementById('partstotalwithtax'); // inputp field for taxes + total value
var totalResult = myResult + myBox2; // totalresult = taxes on part + the parts total
result2.value = totalResult; // display the results


Here's the fiddle:



Problem with + operator and variable types.

In your case you try to add string like this

var a = "1" + "2"; <- 12

What you need is to use ParseFloat and then do math

var a = parseFloat("2") + parseFloat("2.14") <- 4.14

Hope this helps.