Event Listener Status of Dynamically Removed/Created Element

894359
894359 used Ask the Experts™
on
What happens to an event listener which was attached to an element that gets dynamically removed and then dynamically recreated?  If I have a form and an event listener is attached to an element by ID (e.g. ID="f1") and then that element is removed while the even listener is attached;, will the event listener automatically go away or error? Additionally what happens if the element reappears (is recreated dynamically) after the initial attachment? This is in regards to the sequence below.

1) event listener A is attached to element with ID= "f1"
2) element "f1" is dynamically removed while event listener is attached
3) element "f1" is dynamically recreated

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer
Commented:
Every element created in your DOM keeps their properties and events. Even if it's moved to another place in the document.

Every element removed and created again, even using the same ID and content, does not keep events attached (is another object)

See this Codepen example https://codepen.io/fraigo/pen/maMYyX

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial