tags:

views:

49

answers:

2

I want to check if a table body (tbody) is empty using jQuery. So far I've tried something like:

$("#searchTable tbody").is(":empty")

But it doesn't work. Any other ideas?

HTML Sample

<table id="searchTable"cellpadding="0" cellspacing="0" width="100%" >
<thead><tr>
<th>No.</th>
<th><a href="#">Status</a></th>
<th><a href="#" class="asc">Category</a></th>
<th><a href="#" class="desc">Title</a></th>
<th><a href="#" class="desc">Last Used</a></th>
<th><a href="#" class="desc">URL</a></th>
<th style="width: 96px;">Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

So i want to check if something was appended in tbody...if not then alert something.

A: 

If the element is not found, it will return false too. Check is the element is present and is selectable using your selector.

Teja Kantamneni
A: 

You could try something like

$('#searchTable tbody').children().length;

Which will be 0 if tbody is empty (has no children).

Bob
That's exactly what `.is(":empty")` does, right? Or do text nodes not count as children? The OP's error is more likely in flow than in the `:empty` selector not doing its job correctly.
Matchu
not working....
Just tried it out, and .children().length does not count text nodes. If you .append('text'), then .children().length will give zero, while if you append('<tr><td>text</td></tr>'), then children().length will give one. In either case, .is(':empty') returns false.It's worked for me, how exactly is it not working for you? "Not working..." isn't exactly helpful.
Bob