Hello-World
Hello-World

Reputation: 9555

show only the table rows that contain <td> and have a checked check box?

How do I show only the table rows that contain <td> and have a checked check box?

var myTable = '#testTable';

$(myTable).find('tr').has('checkbox:checked').has('td').show();

Thanks

Upvotes: 0

Views: 310

Answers (5)

Alexander Taran
Alexander Taran

Reputation: 6725

$('#testTable>tr>td>input[type=checkbox]:checked').closest('tr').show();

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137567

Doing this efficiently depends on the fact that you traverse the node you want to manipulate in the XPath query. This means that making all subsequent XPath query should actually be done as a filtering sub-search. That's pretty trivial to do; putting square brackets around it does the trick (and adding a . because of the use of >):

$('#testTable>tr[.>td>input[type=checkbox]:checked]').show();

Upvotes: 1

trapper
trapper

Reputation: 11993

You want to show/hide the row not just the cell right?

Need to use .parent().parent() to step back up to tr element.

$('#testTable>tr>td>input[type=checkbox]:checked').parent().parent().show();

Upvotes: 1

Akhil Sekharan
Akhil Sekharan

Reputation: 12683

Is it:

var myTable = '#testTable';
$(myTable).find('tr').each(function(){
    if($(this).find('checkbox:checked').length)
        $(this).show();
});

Upvotes: 0

aleation
aleation

Reputation: 4844

try this:

$('#testTable tr td input[type=checkbox]:checked').show();

$('#testTable>tr>td>input[type=checkbox]:checked').show(); // if you want to make sure they are the direct descendants (only first descendant level)

Upvotes: 1

Related Questions