Please forgive me, I’m new to web programming. I just started with jQuery yesterday and I need a bit of help. I need to make sure that a user cannot delete the last row of a table. I am able to use the following successfully:
$(this).closest('tr:not(:only-child)').remove();
But I would like to display an alert message if the row is the last row instead of just not doing anything. I tried the following, but it did not work:
if( $(this).closest('tr:only-child') ) {
alert('cannot delete last row');
}
else {
$(this).closest('tr').remove();
}
Try: