-1

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);
}
Ariful Haque
  • 3,662
  • 5
  • 37
  • 59

2 Answers2

1

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;
}
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
  • I will add to the line of code?...or i will have a new function?...Sorry im just new in JS – Juan dela Cruz Jun 13 '15 at 09:20
  • how do i call that to display in the field? – Juan dela Cruz Jun 13 '15 at 09:22
  • what if i want to have anothjer function for discount because i want to compute the sub total and the discount to have a grand total... – Juan dela Cruz Jun 13 '15 at 09:25
  • function calculateGrandTotal() { var subTotal = 0; $('table tbody tr td:nth-child(4) input').each(function (index) { subTotal += parseInt($(this).val()); }); $('#sub_total').val(subTotal); $('#discount').val(subTotal*0.1); } – Juan dela Cruz Jun 13 '15 at 12:33
  • if its woking what's the problem? – Sunil B N Jun 13 '15 at 12:53
  • yes..but how would i use the two,.sub total and the discount?...because i have a grand total field...can you visit this link http://chocolatehillsadventurepark.com/reservation.php – Juan dela Cruz Jun 13 '15 at 12:58
  • You need to call this function wherever there is a change in the inputs. So that the changes reflect in the Ui. Your website is full of ads! It's cumbersome to look into – Sunil B N Jun 14 '15 at 03:30
0

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

Sam
  • 41
  • 2
  • 10