views:

64

answers:

3

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?

+4  A: 

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

Skilldrick
Didn't knew about live(), thanks.
planetp
note that `live()` isn't some jQuery magic: it's just an application of event delegation, which can be done without external libraries; to make it usable, you'll basically have to write a cross-browser wrapper yourself, though
Christoph
A: 

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

luca
+2  A: 

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

Matt Dearing