Coder Perfect

In an HTML table, you can hide or show columns.


I have an HTML table with numerous columns, and I need to use jquery to create a column chooser. I want to hide/show the corresponding column in the table when a user clicks on a checkbox. I’d like to achieve this without having to add a class to every td in the table; is there a way to use jquery to pick an entire column? An example of HTML is shown below.

        <tr><th class="col1">Header 1</th><th class="col2">Header 2</th><th class="col3">Header 3</th></tr>

    <input type="checkbox" name="col1" checked="checked" /> Hide/Show Column 1 <br />
    <input type="checkbox" name="col2" checked="checked" /> Hide/Show Column 2 <br />
    <input type="checkbox" name="col3" checked="checked" /> Hide/Show Column 3 <br />

Asked by Brian Fisher

Solution #1

One line of jQuery code to conceal the second column:


Use this if your table has a header(th):


With a single line of jQuery code, you can hide a table column.

To try the code, go to

Take a look at my blog post for an example of an excellent use case:

Using jQuery, you may hide a table column and colorize rows based on its value.

Answered by Leniel Maccaferri

Solution #2

Personally, I prefer the method of a class on each td/th/col. Then, using a single write to className on the container, you can toggle columns on and off, assuming style rules like:

table.hide1 .col1 { display: none; }
table.hide2 .col2 { display: none; }

This will be faster than any JS loop technique, and it will make a considerable difference in responsiveness for particularly long tables.

You might use adjacency selectors to avoid needing to add the class attributes to tds if you can get away with not supporting IE6. Alternatively, if you’re concerned about making the markup cleaner, you might add them automatically in an initialisation stage using JavaScript.

Answered by bobince

Solution #3

Here’s a more advanced answer that allows for user involvement on a column-by-column basis. If this is going to be a dynamic experience, each column should have a clickable toggle that displays the capacity to conceal it, as well as a mechanism to restore previously hidden columns.

In JavaScript, it would look something like this:

  var $btn = $(this);
  var $cell = $btn.closest('th,td')
  var $table = $btn.closest('table')

  // get cell location -
  var cellIndex = $cell[0].cellIndex + 1;

  $table.find("tbody tr, thead tr")

$(".show-column-footer").click(function(e) {
    var $table = $(this).closest('table')
    $table.find("th, td").show()


We’ll add some markup to the table to help with this. We may add something like this to each column header to provide a visual indicator of something clickable.

<button class="pull-right btn btn-default btn-condensed hide-column" 
            data-toggle="tooltip" data-placement="bottom" title="Hide Column">
    <i class="fa fa-eye-slash"></i>  

A link in the table footer will allow the user to restore columns. Toggling it on dynamically in the header may cause it to jostle around the table if it isn’t permanent by default, but you may put it anywhere you want:

<tfoot class="show-column-footer">
    <th colspan="4"><a class="show-column" href="#">Some columns hidden - click to show all</a></th>

That’s all there is to it in terms of functionality. Here’s a video of a demo with a few additional details added. You can also add a tooltip to the button to better define its purpose, style the button more organically to a table header, and collapse the column width to add some (a bit goofy) css animations to smooth out the transition.

Answered by KyleMit

Solution #4

Colgroups could be used:

       <col class="visible_class"/>
       <col class="visible_class"/>
       <col class="invisible_class"/>  
        <tr><th class="col1">Header 1</th><th class="col2">Header 2</th><th class="col3">Header 3</th></tr>

Your script might then modify only the desired col> class.

Answered by Luis Melgratti

Solution #5

It should be as follows:

$("input[type='checkbox']").click(function() {
    var index = $(this).attr('name').substr(2);
    $('table tr').each(function() { 
        $('td:eq(' + index + ')',this).toggle();

This code has not been tested, but the idea is that you select the table cell in each row that matches to the index retrieved from the checkbox name. You could, of course, use a class or an ID to limit the selectors.

Answered by Eran Galperin

Post is based on