tags:

views:

53

answers:

3

How can we hide the column of the table by using jquery

< table >
  < tr >
   < td id="td_1" >name</ td >
   < td id="td_2" >title</ td >
   < td id="td_3" >desc</ td >
  </ tr >
  < tr >
   < td id="td_1" >Dave</ td >
   < td id="td_2" >WEB DEV</ td >
   < td id="td_3" >Blah Blah</ td >
  < /tr >
  < tr >
   < td id="td_1" >Nick< /td >
   < td id="td_2" >SEO< /td >
   < td id="td_3" >Blah Blah and blah< /td >
  < /tr >
< /table >

So suppose if someone want to hide first column i.e. td_1 from all rows, then what will be the code ?

Thanks in Advance Dave

+4  A: 
$(document).ready(function() {
    $("#td_1").hide();
}

But ideally, you want to use a class instead of an ID.

so

<table>
  <tr>
   <td class="td_1">name</td>
   <td class="td_2">title</td>
   <td class="td_3">desc</td>
  </tr>
  <tr>
   <td class="td_1">Dave</td>
   <td class="td_2">WEB DEV</td>
   <td class="td_3">Blah Blah</td>
  </tr>
  <tr>
   <td class="td_1">Nick</td>
   <td class="td_2">SEO</td>
   <td class="td_3">Blah Blah and blah</td>
  </tr>
</table>

And then you would use similar code:

$(document).ready(function() {
    $(".td_1").hide()
}

So the only thing that changed is the hash(#) to a dot(.). Hash is for ID's, Dot is for classes.

Yet another way would be to use the nthChild selector.

$(document).ready(function() {
    $('tr td:nth-child(1)').hide();
}

Where 1 is the column number to be hidden.

HTH

Marko
Thanks for the gr8 idea, I'll use it some where else. But I forgot to mention that Those columns are dynamic, and their id gets increment . so how can I apply the css dynamically ? . Thanks
dave
jquery has great support for css selectors. You can write a selector like $('tr td:nth-child(1)') to select columns... http://api.jquery.com/nth-child-selector/
pxl
I was going to mention that but you beat me to it :) +1
Marko
those jquery guys are geniuses!
pxl
John Resig is the Einstein of Javascript
Marko
Thank you all , I got the idea how to use nth child :) that'll solve my problem
dave
A: 

http://api.jquery.com/hide/

Amit