Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:26:42+00:00 2026-05-20T09:26:42+00:00

I need to provide users with the ability to hide/show table columns. Using previous

  • 0

I need to provide users with the ability to hide/show table columns. Using previous examples with my own adjustments i have used checkboxes to hide/show columns, but now need to remove form elements.

I’m very new to jquery/javascript so hoping someone can help with info on now how to
1. Hide some columns on load (based on trigger in code)
2. Swap images when column is toggled to give users visual feedback as to which columns are shown/hidden.

I have a fiddle at
http://jsfiddle.net/IllusionVK/DqQFP/14/ which has the following code in it:

Previously used following code

$("input:checkbox:not(:checked)").each(function() {
var column = "table ." + $(this).attr("name");
$(column).hide();
});

$("input:checkbox").click(function(){
var column = "table ." + $(this).attr("name");
$(column).toggle();
});
});

but had to remove form elements (checkboxes), and am now trying to use img tag in place of checkbox:

$('img').click(function(){
var column = $(this).attr('id');
$('td:nth-child('+ column +')').toggle();
});

I’m hoping that the final code will be useful for all to hide/show columns without needing to add any code to the actual tables and is using nth-child dynamically.

Any help would be very much appreciated!! Cheers, Charlie.

Sources of help so far:
http://www.devcurry.com/2009/07/hide-table-column-with-single-line-of.html
Uses nth-child, BUT is hardcoded, and designed for only one column show/hide

hide table columns automatically by checking a checkbox with jQuery
But requires that table is overburdened with extra classes=bad for me, use checkboxes=also bad

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:26:43+00:00Added an answer on May 20, 2026 at 9:26 am

    If you use index() you should be able to dynamically show/hide columns if you make a convention that your text of your tags match the table header tags.

    $('span').click(function() {
        var t = $.trim($(this).text());
        var i = $(".headercell td:contains(" + t + ")").index() + 1;
        $('td:nth-child(' + i+ ')').toggle();
    });
    

    Updated jsfiddle.

    side note, a img element can’t have text <img>text</img>, so I adjusted the list to use a span tag instead.

    update
    If you want to easily toggle the legend items you could do something like this..

    <div class="legend">
        <div>Name</div>
        <div>Sex</div>
        <div>Age</div>
    </div>
    

    And the following css:

    .legend div.unchecked
    {
        background-image:url(http://jquery.bassistance.de/validate/demo/images/unchecked.gif);
    }
    .legend div
    {
        background-repeat:no-repeat;
        background-image:url(http://jquery.bassistance.de/validate/demo/images/checked.gif);
        cursor:pointer;
        background-position:left center;
        padding-left:20px;
    }
    

    And your jquery could toggle the class to change the background-image

    $('div.legend div').click(function() {
        $(this).toggleClass("unchecked");
        var t = $.trim($(this).text());
        var i = $(".headercell td:contains(" + t + ")").index() + 1;
        $('td:nth-child(' + i + ')').toggle();
    });
    

    If you want to hide columns on page load simply add a uncheck class to the legend div you want to uncheck.

    <div class="legend">
        <div>Name</div>
        <div>Sex</div>
        <div class="uncheckThis">Age</div>
    </div>
    

    Then running this script will trigger which ones to uncheck.

    $('div.legend div.uncheckThis').click();
    

    Example on jsfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have an app, that you want to provide users ability to browse
I need a user interface that allows users to sort a table according to
I need to provide a window on an ASP.NET MVC web page where registered
I need to provide email sending and receiving capabilities within my java web-application. Think
I am starting research on a project that will need to provide ACID semantics
Do you need to use some kind of provider? Can you setup your own
I am using Microsoft SQL Server 2008. While generating custom reports, I need to
We have a requirement in which we need to change change the words or
I need to modify the open-source FCKeditor (not CKeditor) to allow users to select
I need to display the file system in the NSBrowser. Please provide with some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.