Reputation: 163
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
Reputation: 33238
Change to this:
js
$('table').each(function() {
if($(this).find('tr').children("td").length < 1) {
$(this).hide();
}
});
You want to hide your table? If yes i suggest to use hide()
cause remove()
, removes the element from the DOM.
Upvotes: 5
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