Matt Dunne Matt Dunne - 5 months ago 9
Javascript Question

$(this).closest("tr").remove(); Not working for new elements

I've been battling with this problem for a while but since I'm not seasoned in jQuery, I need a little help.

To put it simply, I'm using buttons to add and remove rows in a table.

I've managed to get this .on() event to fire for the dynamically generated elements, however the

$(this).closest("tr").remove();
only seems to work for the first element before any others are added. What is occurring to make it stop working after new lines are added?

From what I see using other examples here on SO is that I'm using the correct event delegation...

Here's a JSFiddle outlining what I've got so far: https://jsfiddle.net/vsgvLuam/4/

Answer

This will work: jsFiddle

Instead of appending the rows and cells separately, append them all at once so you know the <td>'s are inside the row (<tr>)

Comments