deyanivanov966

jQuery101

Oct 27th, 2021
1,107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.   $(".price,.quantity").on("input", function () {
  3.     let total = 0;
  4.     let quantity = $(this).closest("tr").find(".quantity").val();
  5.     let price = $(this).closest("tr").find(".price").val();
  6.     if (quantity && price) {
  7.       let total = quantity * price;
  8.       let doThis = $(this).closest("tr").find(".total").val(total);
  9.       subTotalSum();
  10.       discountCalc();
  11.     }
  12.   });
  13.  
  14.   let subTotalSum = function () {
  15.     let sum = 0;
  16.     $(".total").each(function () {
  17.       let num = $(this).val();
  18.       if (num) {
  19.         sum += parseFloat(num);
  20.       }
  21.     });
  22.     $(".sub-total").val(sum);
  23.   };
  24.   let discountCalc = function () {
  25.     let subTotal = $(".sub-total").val();
  26.     let discountProcentage = $(".discount").val();
  27.     let calc = (discountProcentage / 100).toFixed(2);
  28.     let multiply = subTotal * calc;
  29.     let discountFinal = subTotal - multiply;
  30.     $(".all-total").val(discountFinal);
  31.   };
  32.  
  33.   $(".btnAdd").click(function () {
  34.     let $tableBody = $(".product-table").find("tbody");
  35.     $trLast = $tableBody.find("tr:last");
  36.     $trNew = $trLast.clone(true);
  37.     $trNew.find("input[type=text]").val("");
  38.     $trNew.find("input[type=number]").val("");
  39.     $trLast.after($trNew);
  40.   });
  41.   $(".discount").keyup(function () {
  42.     $(".total-discount").val($(".discount").val() + "%");
  43.     discountCalc();
  44.   });
  45. });
  46.  
RAW Paste Data