1

I cannot delete the dynamically generated row in a table. I have searched the internet but couldn't found any suitable solution to my problem. I'm sharing what I have done so far:

$("#AddMore").click(function () {
    $("#maintable").each(function () {
        var tds = '<tr>';
        jQuery.each($('tr:last td', this), function () {
            tds += '<td>' + $(this).html() + '</td>';
        });
        tds += '</tr>';
        if ($('tbody', this).length > 0) {
            $('tbody', this).append(tds);
        } else {
            $(this).append(tds);
        }
    });
});
$("#maintable").on('click', "#deleteRow", function () {
    $("#deleteRow").closest('tr').remove();
});

This script is just adding rows but I can't delete the row. I have used jQuery remove function but didn't get any result. Please guide me

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
DevWithSigns
  • 725
  • 16
  • 33

2 Answers2

1

First of all use class instead of id becuase id must be unique and then do like this:

$("#maintable").on('click', ".deleteRow", function () {
    $(this).closest('tr').remove();
});
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

You mihgt want to change

$("#deleteRow").closest('tr').remove();

To

$(this).closest('tr').remove();
naota
  • 4,695
  • 1
  • 18
  • 21