Matt Dunne Matt Dunne - 3 months ago 6x
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

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:


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>)