title: How to find row wise total and grand total for dynamic rows using nested form?
body: <p>I have used nested form. This is my jquery code to find row wise total and grand total for invoice application.</p>
  
  <pre><code>$("tr.sum_hours td").on("change", '.hr', function() {
    row = $(this).parent("td").parent();
    total    = 0;
    qnt      = 0;
    rate     = 0;
    discount = 0;
    tax      = 0;
    $(row).find("td input.hr").each(function(index) {
      if ($(this).val() !== "") {
        if ($(this).hasClass('quantity')){
          qnt = parseFloat($(this).val());
        }
        if($(this).hasClass('rate')){
          rate = parseFloat($(this).val());
        }
        if($(this).hasClass('discount')){
          discount = parseFloat($(this).val());
        }
        if($(this).hasClass('tax')){
          tax = parseFloat($(this).val());
        }
        return total = ((qnt*rate)-(discount)+(tax))
      }
    });
    $(row).find("td input.total").val(total);
    grand_total = 0;
    return $(".total").each(function() {
      if ($("#invoice_grand_total") !== "") {
        grand_total = grand_total + parseFloat($(this).val());
      }
      return $("#invoice_grand_total").val(grand_total);
    });
    });
  </code></pre>
  
  <p>and this is my html code.</p>
  
  <pre><code>&lt;table class='item_details'&gt;
    &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Item name&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
      &lt;th&gt;Qty&lt;/th&gt;
      &lt;th&gt;Rate&lt;/th&gt;
      &lt;th&gt;Discount&lt;/th&gt;
      &lt;th&gt;Tax&lt;/th&gt;
      &lt;th&gt;Total&lt;/th&gt;
      &lt;th&gt;Action&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody class='item_details_input'&gt;
  
      &lt;tr class='sum_hours'&gt;
  &lt;td&gt;&lt;input id="invoice_item_details_attributes_0_item_name" name="invoice[item_details_attributes][0][item_name]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input id="invoice_item_details_attributes_0_description" name="invoice[item_details_attributes][0][description]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="quantity hr" id="invoice_item_details_attributes_0_quantity" name="invoice[item_details_attributes][0][quantity]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="rate hr" id="invoice_item_details_attributes_0_rate" name="invoice[item_details_attributes][0][rate]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="discount hr" id="invoice_item_details_attributes_0_discount" name="invoice[item_details_attributes][0][discount]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="tax hr" id="invoice_item_details_attributes_0_tax" name="invoice[item_details_attributes][0][tax]" size="30" type="text" /&gt;&lt;/td&gt;
  
  &lt;td&gt;&lt;input class="total" disabled="disabled" id="invoice_item_details_attributes_0_total" name="invoice[item_details_attributes][0][total]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;a href="#" class="remove_fields btn btn-mini btn-danger"&gt;remove&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt; 
      &lt;tr class='sum_hours'&gt;
  &lt;td&gt;&lt;input id="invoice_item_details_attributes_1_item_name" name="invoice[item_details_attributes][1][item_name]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input id="invoice_item_details_attributes_1_description" name="invoice[item_details_attributes][1][description]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="quantity hr" id="invoice_item_details_attributes_1_quantity" name="invoice[item_details_attributes][1][quantity]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="rate hr" id="invoice_item_details_attributes_1_rate" name="invoice[item_details_attributes][1][rate]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="discount hr" id="invoice_item_details_attributes_1_discount" name="invoice[item_details_attributes][1][discount]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input class="tax hr" id="invoice_item_details_attributes_1_tax" name="invoice[item_details_attributes][1][tax]" size="30" type="text" /&gt;&lt;/td&gt;
  
  &lt;td&gt;&lt;input class="total" disabled="disabled" id="invoice_item_details_attributes_1_total" name="invoice[item_details_attributes][1][total]" size="30" type="text" /&gt;&lt;/td&gt;
  &lt;td&gt;&lt;a href="#" class="remove_fields btn btn-mini btn-danger"&gt;remove&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>this code is working for static rows. But it is not working for the dynamically added rows.
  Please help.</p>