help with column total display

help with column total display

Hi every one,

   I have two html tables parts and items that the rows a added dynamically, with the same code  the tables add and remove the rows but the the total for the item table displays in the total text box for the parts table, also can't get them to subtract the total when a row is deleted here is the full code


  1. <input type="checkbox" id="chkparts" />  <label>Parts </label> 
    <div id="dvParts" style="display: none">
      <form id="parts">
      <table id="purchaseItems" name="purchaseItems" align="center">
      <tr>
        <th>Part #</th>
        <th>Part Name</th>
        <th>Type</th>
        <th>Qty</th>
        <th>Unit Price</th>
        <th>Amount</th>
        <th>Actions</th>
      </tr>
      <tr>
      <td>
          <input type="text" name="number[]" class="next" onfocus="this.value=''"/>
        </td>
      <td>
          <input type="text" name="name[]" class="next" required  />
        </td>
        <td>
          <select name="type[]" class="next" required>
          <option value=" selected="selected"">Please Select..</option>
          <option value="Code">Code</option>
          <option value="Regular">Regular</option>
        </select>
        </td>
        <td>
          <input type="text" name="quantity[]" class="qty" width=": 50px" required onfocus="this.value=''"/>
        </td>
        <td>
          <input type="text" name="price[]" class="next" required onfocus="this.value=''" />
        </td>
        <td>
          <input type="text" name="amount[]" class="next last" required readonly/>
        </td>
        <td>
          <input type="button" name="addRow[]" class="add" value='+' />
          <input type="button" name="addRow[]" class="removeRow" value='-' />
        </td>
      </tr>
     
    </table>
    <table id="prtTotals" align="center">
               <tr>
                    <th>Total</th><td>
                    <input type="text" name="prtstotal" id="prtstotal" readonly />
                 </td>
               </tr>
              
           
           
            </table>
            </form>
        </div>
       
        <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
      <script type="text/javascript">

      $(document).ready(function() {
      $(document).on('click', '#purchaseItems .add', function() {
        var row = $(this).parents('tr');
        var clone = row.clone();

        // clear the values
        var tr = clone.closest('tr');
        tr.find('input[type=text]').val('');

        $(this).closest('tr').after(clone);
        var tot = 0;
        $(".last").each(function() {
          if (!$(this).val() == '') {
            tot = tot + parseFloat($(this).val());
          }
        })
        $("#prtstotal").html("$" + tot +".00");
      });
      $(document).on("blur", ".last", function() {
        var tot = 0;
        $(".last").each(function() {
          if (!$(this).val() == '') {
            tot = tot + parseFloat($(this).val());
          }
        })
        $("#prtstotal").html("$" +tot+".00");
        document.getElementById("prtstotal").value ="$" +tot+".00";
      });
      $(document).on('focus', ".last", function() {
        var $qty = $(this).parents("tr").find("input[name^='quantity']");
        var $pr = $(this).parents("tr").find("input[name^='price']");
        var $amnt = $(this).parents("tr").find("input[name^='amount[]']");
        var a = 0;
        if ($qty.val() == '' || $pr.val() == '') {
          console.log("No values found.");
          return false;
        } else {
          console.log("Converting: ", $qty.val(), $pr.val());
          var q = parseInt($qty.val());
          var p = parseFloat($pr.val());
          console.log("Values found: ", q, p);
        }
        a = q* p;
        $amnt.val(Math.round(a * 100) / 100);
       
      });
      $(document).on('click', '#purchaseItems .removeRow', function() {
        if ($('#purchaseItems .add').length > 1) {
          $(this).closest('tr').remove();
        }
      });
    });

        </script>

         <input type="checkbox" id="chkNcc" />  <label>Non Covereds </label>
       
    <div id="dvNcc" style="display: none" >
        <form id="ncc">
            <table id="ncItems" name="ncItems" align="center">
                <tr>

                    <th>Type</th>
                    <th>Discription</th>
                    <th>Amount</th>
                    <th>Actions</th>
                </tr>
                <tr>
                    <td>
                        <select name="type" class="next" required>
                            <option value="" selected="selected">Please Select..</option>
                            <option value="Code">Code</option>
                            <option value="Mod">Mod</option>
                        </select>
                    </td>
                    <td>
                        <input type="text" name="discription" class="next" onfocus="this.value='' " required />
                    </td>

                    <td>
                        <input type="text" name="namount" class="next last" onfocus="this.value='' " required/>
                    </td>
                    <td>
                        <input type="button" name="addRow" class="addItem" value='+' />
                        <input type="button" name="remove" class="removeRowItem" value='-' />
                    </td>
                </tr>
        
            </table>
            <table id="ncTotals" align="center">
               <tr>
                    <th>Total</th><td>
                    <input type="text" name="nctotal" id="nctotal" readonly />
                 </td>
               </tr>
              
           
           
            </table>
        </form>
    </div>
          <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">

            $(document).ready(function() {

                $(document).on('click', '#ncItems .addItem', function() {
                    var row = $(this).parents('tr');
                    var clone = row.clone();

                    // clear the values
                    var tr = clone.closest('tr');
                    tr.find('input[type=text]').val('');

                    $(this).closest('tr').after(clone);
                    var total = 0;
                    $(".last").each(function() {
                        if (!$(this).val() == '') {
                            total = total + parseFloat($(this).val());
                        }
                    })
                    $("#nctotal").html("$" + total1 +".00");
                });
                $(document).on("blur", ".last", function() {
                    var total1 = 0;
                    $(".last").each(function() {
                        if (!$(this).val() == '') {
                            total = total + parseFloat($(this).val());
                        }
                    })
                  
                    document.getElementById("nctotal").value ="$" +total1+".00";
                });
                $(document).on('focus', ".last", function() {
                    var $qty = $(this).parents("tr").find("input[name^='quantity']");
                    var $pr = $(this).parents("tr").find("input[name^=discription]");
                    var $amnt = $(this).parents("tr").find("input[name^='namount']");
                    var a = 0;
                    if ($qty.val() == '' || $pr.val() == '') {
                        console.log("No values found.");
                        return false;
                    } else {
                        console.log("Converting: ", $qty.val(), $pr.val());
                        var q = parseInt($qty.val());
                        var p = parseFloat($pr.val());
                        console.log("Values found: ", q, p);
                    }
                    a = q * p;
                    $amnt.val(Math.round(a * 100) / 100);

                });
                $(document).on('click', '.removeRowItem', function() {
                    if ($('#ncItems .addItem').length > 1) {
                        $(this).closest('tr').remove();
                    }
                });
            });
        </script>
  thank you