title: Jquery - Add Column left or right to previous Column
body: <p>I have two buttons called "Add Column Left" and "Add Column Right". I need to get the Index of the clicked cell, so that I can get the column index and add the new column to the previous one.</p>
  
  <p>My current code:</p>
  
  <pre><code>function AddColumn(addLeft){ // Which button is clicked? Add Left || Add Right
  
    var header = $('#tableHeader'); // Get the table header element
    var headerContentToAppend = "&lt;th&gt;Header&lt;/th&gt;"; // The default text of the new header
    var table = $('#tableBody'); // Get the table body element
    var rowContentToAppend = "&lt;td&gt;Content&lt;/td&gt;"; // The default text of the new cells
  
    // issue 1: missing index of the clicked/focussed column
  
    if (addLeft) { // Button "Add Column Left" clicked
      header.prepend(headerContentToAppend); // Add the Column to the left of the previous column
    }
    else { // Button "Add Column Right" clicked
      header.append(headerContentToAppend); // Add the Column to the right of the previous column
    }
  
    $('table th').last().attr("contenteditable", true).focus(); // set all cells editable and focus the last one
    table.find("tr").each(function() { // Get all existing rows ....
  
          // issue 2: maybe the cells don't match to the right column, I have to test this after finishing the first issue
  
            $(this).append(rowContentToAppend); // ... and add cells to the new column
        });
  }
  </code></pre>
  
  <p>So my problem is  to get the right index of the clicked column. After adding a new column i have to fill it up with new cells below.</p>
  
  <p>I placed two comments called "issue" in the code, that should make things clear.</p>
  
  <p>My HTML Code:</p>
  
  <pre><code>  &lt;table id="dataTable"&gt;
      &lt;thead id="tableHeader"&gt;
      &lt;/thead&gt;
      &lt;tbody id="tableBody"&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  </code></pre>
  
  <p>I want to control the table by Javascript to keep it dynamic.</p>