Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are creating multiple ids that are the same. You will want to do a number of things.</p> <ol> <li>Create an incrementor so that the ids are unique</li> <li>Add listeners when they add a new row for the new elements</li> </ol> <p><a href="http://jsfiddle.net/vXHa6/3/" rel="nofollow">fiddle</a></p> <p>Updated JS:</p> <pre><code>$(function() { var INC = 0; // Append Invoice Line $('#addnewitem').click(function() { INC++; var currentTable = $(this).closest('table').attr('id'); $('#' + currentTable).append('&lt;tr&gt;&lt;td&gt;&lt;div class="col-sm-6 col-lg-12"&gt;&lt;input type="Client Name" class="form-control" id="c_name_' + INC + '" placeholder="Item"&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="col-sm-6 col-lg-12"&gt;&lt;textarea class="form-control" placeholder="Description" rows="2" &gt; &lt;/textarea&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="col-sm-6 col-lg-12"&gt;&lt;input type="Client Name" class="form-control" id="item_price_' + INC + '" placeholder="Item Price" name="item_price"&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="col-sm-6 col-lg-12"&gt;&lt;select name="tax" class="form-control" id="tax_' + INC + '"&gt;&lt;option value="0"&gt;None&lt;/option&gt;&lt;option value="12.5"&gt;(12.5%) Service Tax &lt;/option&gt;&lt;/select&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="col-sm-6 col-lg-12"&gt;&lt;input type="Client Name" class="form-control price" id="item_tax_' + INC + '" placeholder="Tax Amount" name="item_tax"&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div class="col-sm-6 col-lg-12"&gt;&lt;input type="Client Name" class="form-control price" id="sub_total_' + INC + '" placeholder="Sub Total" name="sub_total_' + INC + '"&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;button type="button" id="removeItem_' + INC + '" class="btn btn-default removeItem"&gt;&lt;span class="glyphicon glyphicon-trash"&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;').promise().done(function() { $('#sub_total_' + INC).on('change', calculateSum); $('#tax_' + INC).on('change', function() { doMain(INC) }); }); }); //Remove Invoice Line $('#removeItem').click(function() { var currentTable = $(this).closest('table').attr('id'); $(this).closest('tr').remove(); calculateTableSum(currentTable); calculateTotal(); }); function calculateTotal() { } function calculateSum() { var currentTable = $(this).closest('table').attr('id'); calculateTableSum(currentTable); } function calculateTableSum(currentTable) { var sum = 0; $('#' + currentTable + ' input[id^=sub_total]').each(function() { //add only if the value is number if (!isNaN($(this).val()) &amp;&amp; $(this).val().length != 0) { sum += parseFloat($(this).val()); } }); //.toFixed() method will roundoff the final sum to 2 decimal places $('#' + currentTable + ' input.sumamtcollected').val(sum.toFixed(2)); } $('#sub_total').on('change', calculateSum); function doMain(num) { if (num === 0) { var currentTable = $(this).closest('table').attr('id'); var itemprice = $('#item_price').val(); var taxrate = $('#tax').val(); var tax = taxrate * itemprice / 100; var total = parseFloat(itemprice) + parseFloat(tax); $('#item_tax').val(tax.toFixed(2)); $('#sub_total').val(total.toFixed(2)); calculateTableSum(currentTable); calculateTotal(); } else { var currentTable = $(this).closest('table').attr('id'); var itemprice = $('#item_price_' + num).val(); var taxrate = $('#tax_' + num).val(); var tax = taxrate * itemprice / 100; var total = parseFloat(itemprice) + parseFloat(tax); $('#item_tax_' + num).val(tax.toFixed(2)); $('#sub_total_' + num).val(total.toFixed(2)); calculateTableSum(currentTable); calculateTotal(); } } $('#tax').on('change', function() { doMain(0); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload