Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery hide <tr> based on <td> id

I would like to hide some table row from my table based on the table data id, I've only found ways to hide the table row based on the table data value but that is (in my case) not the solution.

For example, lets say this is my table:

<table id='table1' border="1">
<tr id='hideme'>
<td id='letmehide'>row 1, cell 1</td>
<td id='letmehide'>row 1, cell 2</td>
</tr>
<tr id='donthideme'>
<td id='dontletmehide'>row 2, cell 1</td>
<td id='dontletmehide'>row 2, cell 2</td>
</tr>
</table>

What should my javascript/JQuery look like? Fiddle: http://jsfiddle.net/twyqS/

like image 770
BjornBogers Avatar asked Aug 14 '13 13:08

BjornBogers


4 Answers

Take into account that ids must be unique on your page so I recommend you to use css classes instead of ids

HTML

<table id='table1' border="1">
<tr class='hideme'>
<td class='letmehide'>row 1, cell 1</td>
<td class='letmehide'>row 1, cell 2</td>
</tr>
<tr class='donthideme'>
<td class='dontletmehide'>row 2, cell 1</td>
<td class='dontletmehide'>row 2, cell 2</td>
</tr>
</table>

JS

$('#table1 .hideme').hide();

This would allow you to hide several rows if necessary.

like image 86
Claudio Redi Avatar answered Nov 15 '22 17:11

Claudio Redi


First of all is you're using jQuery, use it to attach your event handlers. It results in much more semantic HTML, and is a better separation of concerns.

With regard to your problem you should use the :first selector to get the first tr element, and then the hide() function:

$('#table1 tr:first').hide();

Alternatively you could use the id of the tr as it is intended to be unique:

$('#hideme').hide();

I would suggest familiarising yourself with the jQuery API, specifically the selectors available.

like image 31
Rory McCrossan Avatar answered Nov 15 '22 16:11

Rory McCrossan


Try this:

$('#table1 tr[id="hideme"]').hide();//in table1 hide tr that are with attribute id="hideme"
like image 1
sangram parmar Avatar answered Nov 15 '22 15:11

sangram parmar


ID of an element should be unique, so use class attribute instead of ID in your elements

Try

<table id='table1' border="1">
    <tr class='hideme'>
        <td class='letmehide'>row 1, cell 1</td>
        <td class='letmehide'>row 1, cell 2</td>
    </tr>
    <tr class='donthideme'>
        <td class='dontletmehide'>row 2, cell 1</td>
        <td class='dontletmehide'>row 2, cell 2</td>
    </tr>
</table>

then

$('#table1 tr:has(td.letmehide)').hide()

Demo: Fiddle

like image 1
Arun P Johny Avatar answered Nov 15 '22 16:11

Arun P Johny