title: How do I iterate through table rows and cells in JavaScript and add the values?
body: <p>This is my HTML table, on the table I have 3 rows and 3 columns, what I am trying to do is add the first two columns together and put the result in the third column. Any help would be greatly appreciated.</p>
  
  <p>CSS:</p>
  
  <pre><code>&lt;style type="text/css"&gt;
    .tg  { border-collapse:collapse; border-spacing:0; border-color:#bbb; }
    .tg td { font-family:Arial, sans-serif; font-size:14px; padding:10px 5px;border-
  style:solid; border-width:0px; overflow:hidden; word-break:normal; border-
  color:#bbb; color:#594F4F; background-color:#E0FFEB; }
    .tg th { font-family:Arial, sans-serif; font-size:14px; font-
  weight:normal; padding:10px 5px; border-style:solid; border-
  width:0px; overflow:hidden; word-break:normal; border-
  color:#bbb; color:#493F3F; background-color:#9DE0AD; }
    .tg .tg-jf1j { font-weight:bold; font-size:20px; color:#ffccc9; text-
  align:center; vertical-align:top; }
    .tg .tg-yw4l { vertical-align:top; }
  &lt;/style&gt;
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;table class="tg"&gt;
      &lt;tr&gt;
          &lt;th class="tg-yw4l"&gt;1&lt;/th&gt;
          &lt;th class="tg-yw4l"&gt;1&lt;/th&gt;
          &lt;th id="a"&gt;&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="tg-yw4l"&gt;1&lt;/td&gt;
          &lt;td class="tg-yw4l"&gt;2&lt;/td&gt;
          &lt;td id="a"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="tg-yw4l"&gt;2&lt;/td&gt;
          &lt;td class="tg-yw4l"&gt;3&lt;/td&gt;
          &lt;td id="a"&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Below here is where I try looping through my table and adding the first two columns together.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      var table = document.getElementsByClassName("tg");
  
      for (var r = 0; r &lt; 3; r++) {
          var sum1 = 0;
  
          for (var c = 0; c &lt; 3; r++) {
              if (table[r][c].className == 'tg-yw41') {
                  sum1 += isNaN(table[r][c].innerHTML) ? 0 : parseInt(table[r][c].innerHTML);
              }
          }
  
          document.getElementById('a').innerHTML = sum1;
      }
   &lt;/script&gt;
  </code></pre>