title: jquery table rows line totals and grand total
body: <p>I'm currently writing a small plugin for computer repair techs and I'm now trying to code the invoice section but not having much luck!</p>
  
  <p>I've got the fields being populated from the database but I'm trying to use jquery to update the line totals and the total of the invoice.</p>
  
  <p>Heres a quick image of what I'm trying to do!</p>
  
  <p><img src="https://i.stack.imgur.com/7Galr.png" alt="enter image description here">
  I'm trying to get it to update the totals on pageload and when a value changes in the unit cost and quantity textfields.</p>
  
  <pre><code>$(document).ready(function() {
      $("#invoiceitems tbody tr").each(function() {
          $(this).change(function() {
              updateTotal();
          });
      });
  });
  
  function updateTotal() {
  
      //Calculate Subtotal for each item
      var quantity = $('.quantity').val();
      var price = $('.price').val();
      var subtotal = parseInt(quantity) * parseFloat(price);
      $('.subtotal').text(subtotal);
      //Calculate Grand Total
      var sum = 0;
      $('.linetotal').each(function() {
          sum += parseFloat($(this).text());
      });
      $('#grandTotal').html(parseFloat(sum));
  }​
  </code></pre>
  
  <p>Found this code on here but I really don't have a clue and can't get it to work!</p>
  
  <p>Some direction/help would be greatly appreciated!</p>
  
  <p>Thanks very much
  Jase</p>