title: Using jquery to get per row totals and grand total of table
body: <p>So the short version of this is: Can I traverse only the elements within the matched element of the selectors before the <code>each()</code>? Or is there a simpler way of getting what I want without an <code>each()</code> loop?</p>
  
  <hr>
  
  <p>I thought this would be much easier, which makes me think I'm just missing some fundamental principle of element traversing with jquery.</p>
  
  <p>So here's the scenario:</p>
  
  <p>I have a table (and it is appropriate in this case), where each cell has a text <code>input</code>. The last input is read-only and is supposed to be the total sum of the other values entered on that row. I have a really messy js script for finding both the totals of each row and then the grand total of each row total.</p>
  
  <p>Here's the basic HTML:</p>
  
  <pre><code>&lt;table&gt;
  &lt;thead&gt;
  &lt;tr&gt;&lt;th&gt;Col 1&lt;/th&gt;&lt;th&gt;Col 2&lt;/th&gt;&lt;th&gt;Col 3&lt;/th&gt;&lt;th&gt;Total&lt;/th&gt;&lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  &lt;tr id="row1"&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td class="total"&gt;&lt;input type="text" readonly="readonly" /&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr id="row2"&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td class="total"&gt;&lt;input type="text" readonly="readonly" /&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr id="row3"&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" /&gt;&lt;/td&gt;&lt;td class="total"&gt;&lt;input type="text" readonly="readonly" /&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>The javascript will validate that the data entered is numerical, just to be clear.</p>
  
  <p>So I have a event listener for each input for <code>onchange</code> that updates the total when the user enters data and moves to the next cell/input. Then I have a function called <code>updateTotal</code> that currently uses <code>for</code> loops to loop through each row and within that loop, each cell, and finally sets the input in the total cell to sum. </p>
  
  <p>Quick note: I have included the code below to show that I'm not just looking for a hand out and to demonstrate the basic logic of what I have in mind. Please feel free to skim or skip this part. It works and doesn't need any debugging or critique.</p>
  
  <p>This is what that looks like:</p>
  
  <pre><code>function updateTotal() {
      table = document.getElementsByTagName("tbody")[0];
      allrows = table.getElementsByTagName("tr");
      grandtotal = document.getElementById("grand");
      grandtotal.value = "";
  
      for (i = 0; i &lt; allrows.length; i++) {
          row_cells = allrows[i].getElementsByTagName("input");
          row_total = allrows[i].getElementsByTagName("input")[allrows.length - 2];
          row_total.value = "";
  
          for (ii = 0; ii &lt; row_cells.length - 1; ii++) {
              row_total.value = Number(row_total.value) + Number(row_cells[i][ii].value);
              grandtotal.value = Number(grandtotal.value) + Number(row_cells[i][ii].value);
          }
      }
  }
  </code></pre>
  
  <p>Now I am trying to re-write the above with jquery syntax, but I'm getting stuck. I thought the best way to go would be to use <code>each()</code> loops along the lines of:</p>
  
  <pre><code>function findTotals() {
  $("tbody tr").each(function() {
      row_total = 0; 
      $($(this) + " td:not(.total) input:text").each(function() {
          row_total += Number($(this).val());
          }); 
      $($(this) + " .total :input:text").val(row_total);
  });
  
  }
  </code></pre>
  
  <p>But using <code>$(this)</code> doesn't seem to work in the way I thought. I read up and saw that the use of <code>$(this)</code> in an each loop points to each matched element, which is what I expected, but I don't get how I can traverse through that element in the each() function. The above also leaves out the grand_total bit, because I was having even less luck with getting the grand total variable to work. I tried the following just to get the row_totals:</p>
  
  <pre><code> $($(this).attr("id") + " td:not(.total) input:text").each(function() {
  </code></pre>
  
  <p>with some success, but then managed to break it when I tried adding on to it. I wouldn't think I'd need each row to have an id to make this work, since the each part should point to the row I have in mind.</p>
  
  <hr>
  
  <p>So the short version of this is: Can I use the each loop to traverse only the elements within the matches, and if so, what is the correct syntax? Or is there a simpler way of getting what I want without an each loop?</p>
  
  <p>Oh, one last thought...</p>
  
  <p>Is it possible to get the numerical sum (as opposed to one long string) of all matched elements with jquery? I'll research this more myself, but if anyone knows, it would make some of this much easier.</p>