javascript calculate 10% discount on sub total

994 views Asked by At

How to implement calculating 10% discount base on sub total in js?. Here is my code.

<!-- Sub Total -->
function calculateGrandTotal() {
    var grandTotal = 0;
    $('table tbody tr td:nth-child(4) input').each(function (index) {
                                grandTotal += parseInt($(this).val());
    });
    $('#sub_total').val(grandTotal);
}
2

There are 2 answers

0
Sam On

Use getElementbyID to obtain value of your subtotal field and apply the formula.

7
Sunil B N On

It would look like this:

function calculateGrandTotal() {
                            var grandTotal = 0;
                            $('table tbody tr td:nth-child(4) input').each(function (index) {
                                grandTotal += parseInt($(this).val());
                            });
                            $('#sub_total').val(grandTotal);
                            $('#discount').val(calculateDiscount(grandTotal));
 } 
//separate function to calculate discount
function calculateDiscount(amt){
   if(!NaN(amt)){
      return amt*0.1;//10% disc
   }else return 0;
}