title: Append columns to an HTML table offsetting from right
body: <p>How can I count the number of columns in the first row of a table, subtract by one and then insert a column at that location into my table? I would like the first row of that column to be different than the rest of them.</p>
  
  <p><a href="http://jsfiddle.net/yBDUa/" rel="nofollow noreferrer">Here is a basic fiddle that inserts columns into a table at a fixed location.</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1655319/inserting-a-table-column-with-jquery">This topic discusses inserting new columns.</a> Problem is that it uses a fixed, from the left, position to insert the column. It also does not change the top column.</p>
  
  <p>Here is my sample code adapted from that post:</p>
  
  <pre><code>$(document).ready(function(){
     $('#AddOT').click(function(){
        $('#Table1').find('tr').each(function(){
            $(this).find('td').eq(0).after('&lt;td&gt;cell 1a&lt;/td&gt;');
        });
      });
   });
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/6683882/jquery-how-to-count-table-columns">This topic discusses determining the number of columns in "tables".</a> The sample does not seem to work because placing multiple tables in the HTML creates a problem:</p>
  
  <pre><code>    $(function() {
          var colCount = 0;
          $('tr:nth-child(1) td').each(function () {
              if ($(this).attr('colspan')) {
                  colCount += +$(this).attr('colspan');
              } else {
                  colCount++;
              }
          });
      });
  </code></pre>
  
  <p>There is a <a href="http://jsfiddle.net/craigm/XQjFB/" rel="nofollow noreferrer">jsfiddle</a> to go along with the counting of the columns.</p>
  
  <p><strong>EDIT:</strong> Problem is solved and I was able to make <a href="http://jsfiddle.net/eGJvq/2/" rel="nofollow noreferrer">a revised fiddle that places different content into different rows according to the row index value.</a> </p>
  
  <p>This will be very useful as I have different classes of text input boxes on different rows and I sum them according to class. I can now dynamically add new columns to all the rows but still keep unique classes of cells in each row. Awesome!</p>