innerHTML effect on JavaScript

Does innerHTML defunct all JavaScript associated with the element on which its ran or does it defunct only the event listeners associated with the element on which it ran?

Of which „other“ connection between elements and JavaScript do you think if not event listeners?

At the end it is not even removing event listener. Ist only replacing html content. But a event listener in JavaScript is connected to an element in the content. So if you replace an element in the DOM the event listener is not automatically assigned to the new element even if it has the same Id.

That’s why there is something called event delegation for elements, which makes it possible to create event handlers for dynamically changing elements (which means manipulations the DOM in any way like innerHTML() or even html()).

1 Like

Of which „other“ connection between elements and JavaScript do you think if not event listeners?

That’s a good clarification question, I think I don’t know enough about JavaScript to answer (perhaps JavaScript-added-CSS? as with element.style.display = ""; ?).