-2

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
}

screenshot

Here's the fiddle:

https://jsfiddle.net/jdarville/hxqev0be/

JCD
  • 297
  • 2
  • 19

2 Answers2

3

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.

Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
0

You could use the unary + operator for casting to Number.

var myBox1 = +$( 'input[name=tax2]:checked' ).val(); //taxes value
var myBox2 = +document.getElementById('partstotalvalue').value; //parts total 
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392