You are viewing limited content. For full access, please sign in.

Question

Question

Calculate tax based on percentage

asked on November 6, 2015

How do I calculate the tax of the subtotal of all items in table, add it to the subtotal and create a grand total?

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
$(this).find('.subtotal input').val(s);
sum += s;
sum += parseNumber($('.tax input').val());
});
$('.total input').val(sum);
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

 

taxes.PNG
taxes.PNG (10.87 KB)
0 0

Answers

APPROVED ANSWER
replied on November 6, 2015 Show version history

Can you clarify what you want to use the "Tax" field for? Are you wanting to use a specific tax rate to then multiple to the combined subtotal of all items in the table and then display the tax amount in that "Tax" field? And then the "Total" field will be the sum of the combined subtotal with the value in the "Tax" field?

Or are users supposed to input something themselves into the "Tax" field because I see you check for a blur event on that field and then run your function again?

If this is for the first scenario, then you can use something like below

$(document).ready(function() {
 
  $('.tax input').attr('readonly',true);
  $('.total input').attr('readonly',true);
 
  $('.cf-table-block').on('blur', 'input', sumtotal);
  $(document).on('click', '.form-del-field', sumtotal);
 
  function sumtotal() {
    var sum = 0;
    $('.cf-table-block tbody tr').each(function () {
      var s = 0;
      s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
      $(this).find('.subtotal input').val(s.toFixed(2));
      sum += s;
    });
    $('.tax input').val((sum * 0.15).toFixed(2));
    $('.total input').val((parseNumber(sum) + parseNumber($('.tax input').val())).toFixed(2));
  }
 
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
 
});

Note that I made the tax and total fields readonly so it depends solely on the calculation in the table and can't be manipulated otherwise. I also made it so that when a row is deleted from the table, the tax and total gets updated.

0 0
SELECTED ANSWER
replied on November 6, 2015

What if you replace

$('.tax input').val(parseNumber((sum * 0.15).toFixed(2)));

with

$('.tax input').val((sum * 0.15).toFixed(2));

1 0

Replies

replied on November 6, 2015

Thanks again Alex,

Works a charm. one thing I noticed is that the tax field doesn't get the 2 decimal places it should from the code...

 

See below.

 

0 0
replied on November 6, 2015

Can you copy and paste the exact JS code you're using? It works in my example using your inputs and with a hard coded 0.15 tax rate

0 0
replied on November 6, 2015 Show version history
$(document).ready(function() {
 
  $('.tax input').attr('readonly',true);
  $('.total input').attr('readonly',true);
 
  $('.cf-table-block').on('blur', 'input', sumtotal);
  $(document).on('click', '.form-del-field', sumtotal);
 
  function sumtotal() {
    var sum = 0;
    $('.cf-table-block tbody tr').each(function () {
      var s = 0;
      s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
      $(this).find('.subtotal input').val(s.toFixed(2));
      sum += s;
    });
    $('.tax input').val(parseNumber((sum * 0.15).toFixed(2)));
    $('.total input').val((parseNumber(sum) + parseNumber($('.tax input').val())).toFixed(2));
  }
 
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
 
});

 

0 0
SELECTED ANSWER
replied on November 6, 2015

What if you replace

$('.tax input').val(parseNumber((sum * 0.15).toFixed(2)));

with

$('.tax input').val((sum * 0.15).toFixed(2));

1 0
replied on November 6, 2015

It works! Thanks!

0 0
replied on November 6, 2015

You're welcome. I've edited the original answer as well as it has the full example.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.