Pat
Pat

Reputation: 163

how to hide the whole table if there is only 1 row

I have example table below, sometime it has content , but sometime doesn't.

How do I use jquery, so that if there is no td (or there is only 1 row of tr) I will hide the whole table?

example 1:
<table>
<tr><th>some title</th></tr>
</table>

example 2:
<table>
<tr><th>some title</th></tr>
<tr><td>some content</td></tr>
</table>

I tried, but doesn't seem working.

$('table').each(function() {
    if ($(this).find('td:empty').length) $(this).remove();
});​

Upvotes: 1

Views: 4271

Answers (3)

Alex Char
Alex Char

Reputation: 33238

Change to this:

js

$('table').each(function() {
    if($(this).find('tr').children("td").length < 1) {
        $(this).hide();
    }
});

fiddle

You want to hide your table? If yes i suggest to use hide() cause remove(), removes the element from the DOM.

Upvotes: 5

Nathan
Nathan

Reputation: 12010

$('table').each(function() {
    if ( $(this).find('td').length < 1 || $(this).find('td').is(':empty') ) {
       $(this).hide();
    }
});

Example: http://jsfiddle.net/BX46H/1/

Upvotes: 0

Kylie
Kylie

Reputation: 11759

 var len = $('#yourTable tr');
 if(len.length>1){
  $('#yourTable').show();  
 } else {
  $('#yourTable').hide();   
 }

Example : HERE

Upvotes: 3

Related Questions