I'm trying to detect if there are any rows left in a table, once none exist, I will display an alert. The following code doesn't seem to work, any ideas? (There will be one row left with the table headers, which is why I used the index 1)
$('.remove_row').live('click', function() {
//If there are no rows left in table, display alert
if ( $(this).parent().parent().parent().children('tr:eq(1)').length == 0) {
alert("no rows left");
}
$(this).parent().parent().remove();
});
EDIT: I tried with the remove before the alert, and it triggers every time I remove a row.
EDIT AGAIN: Got it, needed to make the index 2, since it is checking before the final one is actually removed from the DOM.
if ( $(this).closest("tbody").find("tr").length === 0 ) {
alert("no rows left");
}
This finds the cloest parent tbody
of the clicked element (a tbody
always exists in the DOM). It checks for the length of the tr
elements within said tbody
. If it equals 0, it will display the alert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With