title: show/hide html table columns using css
body: <p>I want to display a basic html table with controls to toggle showing/hiding of additional columns:</p>
  
  <pre><code>&lt;table id="mytable"&gt;
      &lt;tr&gt;
          &lt;th&gt;Column 1&lt;/th&gt;
          &lt;th class="col1"&gt;1a&lt;/th&gt;
          &lt;th class="col1"&gt;1b&lt;/th&gt;
          &lt;th&gt;Column 2&lt;/th&gt;
          &lt;th class="col2"&gt;2a&lt;/th&gt;
          &lt;th class="col2"&gt;2b&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;100&lt;/td&gt;
          &lt;td class="col1"&gt;40&lt;/td&gt;
          &lt;td class="col1"&gt;60&lt;/td&gt;
          &lt;td&gt;200&lt;/td&gt;
          &lt;td class="col2"&gt;110&lt;/td&gt;
          &lt;td class="col2"&gt;90&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>So Column 1 and Column 2 will be the only columns displayed by default - but when you click on the Column 1  I want 1a and 1b to toggle, and same with Column 2 with 2a and 2b.  I may end up with more columns and lots of rows - so any javascript looping approaches have been too slow to work with when I tested.</p>
  
  <p>The only approach that seems to be fast enough is to set up some css like this:</p>
  
  <pre><code>table.hide1 .col1 { display: none; }
  table.hide2 .col2 { display: none; }
  table.hide3 .col3 { display: none; }
  
  table.show1 .col1 { display: table-cell; }
  table.show2 .col2 { display: table-cell; }
  table.show3 .col3 { display: table-cell; }
  </code></pre>
  
  <p>And then set up onClick function calls on the table header cells that will trigger a toggle - and determine which css class to set "mytable" to that will create the toggle effect that I'm looking for.  Is there an easy way to set this up so that the code can work for n # of columns?</p>
  
  <h2>Update</h2>
  
  <p>Here is what I came up with, works great - and really fast.  Let me know if you can think of ways to improve.</p>
  
  <p>CSS</p>
  
  <pre><code>.col1 {display: none; }
  .col2 {display: none; }
  .col3 {display: none; }
  
  table.show1 .col1 { display: table-cell; }
  table.show2 .col2 { display: table-cell; }
  table.show3 .col3 { display: table-cell; }
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>function toggleColumn(n) {
      var currentClass = document.getElementById("mytable").className;
      if (currentClass.indexOf("show"+n) != -1) {
          document.getElementById("mytable").className = currentClass.replace("show"+n, "");
      }
      else {
          document.getElementById("mytable").className += " " + "show"+n;
      }
  }
  </code></pre>
  
  <p>And the html snippet:</p>
  
  <pre><code>&lt;table id="mytable"&gt;
  &lt;tr&gt;
      &lt;th onclick="toggleColumn(1)"&gt;Col 1 = A + B + C&lt;/th&gt;
      &lt;th class="col1"&gt;A&lt;/th&gt;
      &lt;th class="col1"&gt;B&lt;/th&gt;
      &lt;th class="col1"&gt;C&lt;/th&gt;
      &lt;th onclick="toggleColumn(2)"&gt;Col 2 = D + E + F&lt;/th&gt;
      &lt;th class="col2"&gt;D&lt;/th&gt;
      &lt;th class="col2"&gt;E&lt;/th&gt;
      &lt;th class="col2"&gt;F&lt;/th&gt;
      &lt;th onclick="toggleColumn(3)"&gt;Col 3 = G + H + I&lt;/th&gt;
      &lt;th class="col3"&gt;G&lt;/th&gt;
      &lt;th class="col3"&gt;H&lt;/th&gt;
      &lt;th class="col3"&gt;I&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
      &lt;td&gt;20&lt;/td&gt;
      &lt;td class="col1"&gt;10&lt;/td&gt;
      &lt;td class="col1"&gt;10&lt;/td&gt;
      &lt;td class="col1"&gt;0&lt;/td&gt;
      &lt;td&gt;20&lt;/td&gt;
      &lt;td class="col2"&gt;10&lt;/td&gt;
      &lt;td class="col2"&gt;8&lt;/td&gt;
      &lt;td class="col2"&gt;2&lt;/td&gt;
      &lt;td&gt;20&lt;/td&gt;
      &lt;td class="col3"&gt;10&lt;/td&gt;
      &lt;td class="col3"&gt;8&lt;/td&gt;
      &lt;td class="col3"&gt;2&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>