planetp
planetp

Reputation: 16113

Does removing elements from the DOM affect their event handlers?

If I remove an element from the DOM which has event handlers attached, and subsequently add an element with the same ID somewhere, will the new element have handlers?

Upvotes: 4

Views: 398

Answers (3)

Matt Dearing
Matt Dearing

Reputation: 9386

No, but you can look into event delegation. The main idea is that events will bubble up to parent DOM elements so you can attach your event handler higher up the DOM. You can register for an event like click, and give this handler rules on what it should do with child element click events. This handler will still be there when its children elements are added or removed so there is no need to register event handlers on the individual elements themselves. Here is a link

Upvotes: 2

Luca Rocchi
Luca Rocchi

Reputation: 6484

no, the new element will not have the handler attached , you have to reassign the handler explicity

Upvotes: 0

Skilldrick
Skilldrick

Reputation: 70879

No, because it will be a different object. If you used jQuery and live() you'll get this behaviour.

Upvotes: 6

Related Questions