Skip to content Skip to sidebar Skip to footer

How To Calculate Summation Same Id Of Two Products From Cloned Input Values Jquery

How to do each summation of class three of the products. Here is my similar work. How to do summation of product of two input values from a clone table input values

Solution 1:

This works - note the keyup and the loop over .subTotal every time, not just first time

I have cleaned the code and use the HTML from your other question

functiontotalIt() {
  var total = 0;
  $(".subtotal").each(function() {
    var val = this.value;
    total += val == "" || isNaN(val) ? 0 : parseInt(val);
  });
  $("#total").val(total);
}
$(function() {
  var $to_clone = $('.tr_clone').first().clone();

  $("table").on('click', 'input.tr_clone_add', function() {
    var $tr = $(this).closest('.tr_clone');
    var $clone = $to_clone.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
  });
  $("table").on('click', 'input.tr_clone_remove', function() {
    var $tr = $(this).closest('.tr_clone');
    if ($tr.index() > 1) $tr.remove(); // leave the firsttotalIt();
  });

  $(document).on("keyup", ".quantity, .price", function() {
    var $row = $(this).closest("tr"),
      prce = parseInt($row.find('.price').val()),
      qnty = parseInt($row.find('.quantity').val()),
      subTotal = prce * qnty;
    $row.find('.subtotal').val(isNaN(subTotal) ? 0 : subTotal);
    totalIt()
  });

});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tablewidth="100%"border="0"><thead><tr><th>Item</th><th>Quantity</th><th>U$ Price</th><th>Subtotal</th><th>Add</th><th>Remove</th></tr><trclass="tr_clone"><td><selectstyle="width:200px"name="itens[]"><optionvalue="0"></option><optionvalue="1">Item A</option><optionvalue="2">Item B</option><optionvalue="3">Item C</option></td><td><inputtype="text"size="5"maxlength="5"name="qtd"class="quantity text ui-widget-content ui-corner-all"></td><td><inputtype="text"size="10"maxlength="10"name="price"class="price text ui-widget-content ui-corner-all"></td><td><inputtype="text"size="10"maxlength="10"name="subtotal"class="subtotal text ui-widget-content ui-corner-all"></td><td><inputtype="button"name="add"value="Add"class="tr_clone_add"></td><td><inputtype="button"name="remove"value="Remove"class="tr_clone_remove"></td></tr></table><inputtype="text"readonlyid="total" />

Post a Comment for "How To Calculate Summation Same Id Of Two Products From Cloned Input Values Jquery"