rawatdeepesh
rawatdeepesh

Reputation: 584

hide all td in a table excluding the clicked one

I have a fieldset in which I have a table with multiple tr and td . I have to hide all the td upon click event except the td that is clicked inside the fieldset. I tried traversing to the parent tr of the tds that have to be clicked and then set the css to "display:none" . But it seems to hide all the td instead.

Here is my fiddle. Could this be achieved using the same the same traversals .

           $('#fieldset_QCNew table tbody tr td:nth-child('+index+')').css("display","none"); 

https://jsfiddle.net/rawatdeepesh/dzg68ajk/1/

Upvotes: 0

Views: 1466

Answers (3)

user6841285
user6841285

Reputation:

you need to use

$(this).closest('table').find('td').not(this).toggle();

This should find the parent first and then locare the td inside it and perform operation on the result except the current one.

Upvotes: 1

msvairam
msvairam

Reputation: 872

Pls add the id="td_col" in that td's like

<td id="td_col" style="vertical-align:top;width:25%;">

And the add the below script

$(document.body).on('click', '#td_col', function(event) {

 $("[id^=td_col]").hide();
 $(this).show();

});

Refer the jsfiddle link https://jsfiddle.net/mutvairam/ymysc1sv/

Upvotes: 0

PeterKA
PeterKA

Reputation: 24638

Here is what you need:

$(function() {
    $('td').on('click', function() {
        $(this).closest('table').find('td').not(this).toggle();
    });
});

NOTE: If you'd like the cells to maintain their positions then using .css("visibility", "hidden") rather than .hide() or toggle -- which use .css("display", "none") -- would achieve that.

$(function() {
    $('td').on('click', function() {
        $(this).closest('table').find('td').not(this).toggle();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>Col A</th>
      <th>Col B</th>
      <th>Col C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1,1</td>
      <td>1,2</td>
      <td>1,3</td>
    </tr>
    <tr>
      <td>2,1</td>
      <td>2,2</td>
      <td>2,3</td>
    </tr>
    <tr>
      <td>3,1</td>
      <td>3,2</td>
      <td>3,3</td>
    </tr>
  </tbody>
</table>

Upvotes: 1

Related Questions