0

This is the first time ever, that I have posted on StackOverflow. The reason being, I have always found a solution each time I searched stackoverflow for it.

However this time I am still struggling to find a simple solution like the code I am using here. (Found on W3Schools) I have already looked at some very advanced and complicated Hide/Reveal tables functionalities, but I am after something simply like below. The current code very easily hides a whole when clicked on. I wanted to know, if the same can be applied for a column.

I tried using col, colgroup but it does not work. Can someone please suggest? Also tried applying TH, but that doesn't work too.

PS: I understand HTML & CSS very well, and some very basic PHP, I have used Jquery sparingly, but cannot completely read and understand javascript well enough to make my own modifications or write my own code yet.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("tr").click(function(){
    $(this).hide();
  });
});
</script>
</head>
<body>
<table width="800" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td bgcolor="#6600FF">9</td>
    <td bgcolor="#6600FF">10</td>
    <td bgcolor="#6600FF">11</td>
    <td bgcolor="#6600FF">12</td>
  </tr>
  <tr>
    <td bgcolor="#CCCC66">13</td>
    <td bgcolor="#CCCC66">14</td>
    <td bgcolor="#CCCC66">15</td>
    <td bgcolor="#CCCC66">16</td>
  </tr>
  <tr>
    <td bgcolor="#FF9966">17</td>
    <td bgcolor="#FF9966">18</td>
    <td bgcolor="#FF9966">19</td>
    <td bgcolor="#FF9966">20</td>
  </tr>
  <tr>
    <td bgcolor="#993399">21</td>
    <td bgcolor="#993399">22</td>
    <td bgcolor="#993399">23</td>
    <td bgcolor="#993399">24</td>
  </tr>
</table>

<blockquote>&nbsp;</blockquote>
</body>
</html>
2
  • jQuery / javascript isn't that magic after all: try to think about it this way: what CSS would you add to make a column disappear? jQuery only offers you an way to do this more dynamically $("tr").hide() adds a css-style display:none to all "tr" elements, for example... Commented Oct 15, 2013 at 9:58
  • To hide column, you should first write the click event on td. Get the index no of the td clicked wrt its parent tr. Then on click of td, you have to call hide fn for all the the nth child of TRs. Commented Oct 15, 2013 at 10:04

5 Answers 5

4

Try

$(document).ready(function(){
    $("td").click(function(){
        var idx = $(this).index();
        $('table tr').find('td:eq(' + idx + ')').hide()
    });
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Arun This is exactly what I was looking for! Thanks a Million! Do you have any recommendations for learning how to use JQuery functions?
0

If you want to hide the column N:

$('tr').each(function() {
    $('td', $(this)).eq(N).hide()
});

Note that N = 0 for the first column and N = 3 for the 4th column.

Comments

0

use this code:

$(document).ready(function(){
  $("tr td").click(function(){
    $(this).hide();
  });
});

jsfiddle is here

2 Comments

it won't close the first td in every tr.
Thanks Jai, I tried this, but what I am looking for is to hide the column.
0

Try this: http://jsfiddle.net/CPpYU/

$(document).ready(function () {
    $("td").click(function () {
        var td = this;
        var tr = $(td).parent("tr");
        var column = tr.children().get().indexOf(td);
        console.log(column + "OO");
        $("td:nth-child(" + column + ")").hide();
    });
});

1 Comment

DThought I tried this, but after hide 1 or 2 columns it stops working.
0

Then this would be simple but first you have to understand your task.

You have to remove only td of every column of your table. you have to find td you have click index of that table. For example 00 ,01,11..33 for tow-dimensional table (3*3).

  • if your index is one of them 00,10,20 then hide them all <td>.(first column)
  • if your index is one of them 01,11,21 then hide them all <td>.(second column)
  • if your index is one of them 02,13,24 then hide them all (third column)

Give id to your table <td id="td-00" .

Thanks but I haven't give you complete code but its a simplest solution of your problem. mostly of 5 line code of jQuery.

1 Comment

Thanks Deepak, I understand your logic, but I would not be able to write this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.