Skip to content
Advertisement

how to remove the appended elements in jquery

By right-clicking on each picture, the picture will be removed, and a new item will be created by clicking on the + button.

But the problem is that the new items that are created (appended) could not be removed. Why is this the case?

JavaScript
JavaScript

Advertisement

Answer

Because those aren’t targeted by the event handler. It doesn’t automatically update the elements which are matching the query selector, it’s the same as if you’d used addEventHandler – it runs once. (see below where they don’t log a message to the console but the hardcoded ones do).

JavaScript
JavaScript

Instead what I would do is define that removal as a function and re-add it to the new element using the id:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement