title: Calculating the sum of numbers in contentEditable text using Jquery
body: <p>All right, here's the setup: I have a table in a "form" whose contents I'm editing using the ContentEditable HTML attribute (set on the parent element of the table). There are four columns all together-- two sets of "item description" columns and two sets of matching "price" columns, along with an area at the bottom where I'd like the for the total price to be displayed.</p>
  
  <p>Problem #1 is that I'm not sure how to calculate the sum of numbers across cells when I'm not working with inputs and fields. Complicating things (at least to me) is that I'm adding rows to the table dynamically with Jquery.</p>
  
  <p>Problem #2 is that the second set of description/price columns are optional-- I'd like to be able to use an "Add" button to trigger individual items in those columns to be added to the total.</p>
  
  <p>Edit: I tried some random things and have had some success by storing the values of the cells in an array, then telling Jquery to add the contents of that array and output the sum. The problem now is that it's not giving me a sum, it's giving me the array contents without whitespace. For example, if the array contains 1,2,3 it gives me 123 instead of 6.</p>
  
  <p>Edit #2: After cobbling together bits and pieces of a half dozen tutorials, I've got something that works for Problem #1. Now on to problem #2!</p>
  
  <pre><code>var sumArray = []
              $('#calc').click(function(){ 
                  $('table tbody tr').each(function() {
                      sumArray.push($(this).find('.cost').text().match(/\d+/));                       
                  });
                  var total = 0;
                  for (var i = 0; i &lt; sumArray.length; i++) {
                      total += parseInt(sumArray[i]);
                  }
                  $('.totalcost').text(total);
  
              });
  </code></pre>
  
  <p>Here's the table code:</p>
  
  <pre><code>&lt;table&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                          &lt;th&gt;Service&lt;/th&gt;
                          &lt;th&gt;Cost&lt;/th&gt;
                          &lt;th&gt;Complementary (Optional) Service&lt;/th&gt;
                          &lt;th&gt;Cost&lt;/th&gt;
  
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;/td&gt;
                  &lt;td class="cost"&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;/td&gt;
                      &lt;td class="compcost"&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;tfoot&gt;
                  &lt;tr&gt;
  
                          &lt;th&gt;Total&lt;/th&gt;
                          &lt;th class="totalcost"&gt;&lt;/th&gt;
  
                  &lt;/tr&gt;
              &lt;/tfoot&gt;
          &lt;/table&gt;
          &lt;span id="add"&gt;Add&lt;/span&gt;
          &lt;span id="remove"&gt;Remove&lt;/span&gt;
          &lt;span id="reset"&gt;Reset&lt;/span&gt;
          &lt;span id="nocomp"&gt;No Comps&lt;/span&gt;
          &lt;span id="calc"&gt;Calculate Total&lt;/span&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>And here's the JS I'm using right now.</p>
  
  <pre><code>//Add fields to table
              var i = $('tbody&gt;tr').size() + 1;
  
              $('#add').click(function() {
                  if ($("tbody tr:first-child td").length &gt; 2) {
                  $('&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td class="cost"&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td class="compcost"&gt;&lt;/td&gt;&lt;/tr&gt;').fadeIn('slow').appendTo('tbody');
                  i++;
                  }
                  else
                  {$('&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td class="cost"&gt;&lt;/td&gt;&lt;/tr&gt;').fadeIn('slow').appendTo('tbody');
                  i++;
                  };
  
              });
  
              $('#remove').click(function() {
              if(i &gt; 1) {
                  $('tbody&gt;tr:last').remove();
                  i--;
              }
              });
  
              $('#reset').click(function() {
              while(i &gt; 2) {
                  $('tbody&gt;tr:last').remove();
                  i--;
              }
              });
  
              $('#nocomp').click(function(){
                  if ($("tbody tr td").length &gt; 2) {
                  $('thead tr th:nth-child(2),thead tr th:nth-child(3),tbody&gt;tr td:nth-child(2),tbody&gt;tr td:nth-child(3)').remove();
                  }
              });
  
          /*$('#calc').click(function(){
              $('table tbody tr').each(function() {
              $(this).find('.totalcost').text(
              parseFloat($(this).find('.cost').text())
          );
              });
          });*/
  
          $('#calc').click(function(){ 
              $(this).find('table tbody tr td.cost').each(function(idx)
              {
                  var total = $(this).text();
                  count += total;                          
              });
              alert(parseInt(count));
          });
  </code></pre>