Matt Dunne Matt Dunne - 1 year ago 87
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:

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download