I have this table structure
<table id="latest" class="debt">
<thead>
<tr>
<th width="50">Credor</th>
<th width="50">Devedor</th>
<th>Motivo</th>
<th width="80">Valor</th>
<th width="10"></th>
</tr>
</thead>
<tbody>
<?php foreach ($this->latest as $latest) { ?>
<tr <?php if ($latest->reg_value < 0) { echo "class='error'"; } ?>>
<td><?php echo $latest->findParentRow('Application_Model_DbTable_Names','Creditor')->nam_name; ?></td>
<td><?php echo $latest->findParentRow('Application_Model_DbTable_Names','Debtor')->nam_name; ?></td>
<td><?php echo $latest->reg_reason; ?></td>
<td>R$ <?php echo number_format(abs($latest->reg_value), 2, ',', ' ')?></td>
<td><a href="#" id="<?php echo $latest->reg_id; ?>" class="delete"><img src="http://192.168.0.102/libraries/css/blueprint/plugins/buttons/icons/cross.png" alt=""/></a></td>
</tr>
<?php } ?>
</tbody>
</table>
each row has a delete button which uses AJAX and removes the tr element itself. What happens is: it may happen the tbody become empty when all rows are deleted, when this happens I’d like to delete all the table.
I know there is a pseudo-selector $('#latest tbody:empty')but this selects only the tbody. I want to select the whole table when the tbody is empty or something like it.
EDIT after answers
I inspected the element after deleting all rows: there’s only and the thead tag inside the tag. Still, it didn’t removed the table element. Look at the code
// TR Fading when deleted
$('.delete').live('click', function() {
$.ajax({
type: 'POST',
url: 'history/delete/id/'+$(this).attr('id')
});
$(this).closest('tr').fadeOut('slow', function() {
$(this).remove();
if($(this).closest('table').find('tbody').is(':empty'))
$('#latest').remove();
});
return false;
});
I can’t check if the element is empty or not ’cause it will never be empty for text nodes still remains inside its markup. I have to check if the
<table>has<tr>inside it.