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
2010-03-02 14:43:41
Didn't knew about live(), thanks.
planetp
2010-03-02 14:51:38
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
2010-03-02 15:08:17
A:
no, the new element will not have the handler attached , you have to reassign the handler explicity
luca
2010-03-02 14:46:30
+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
2010-03-02 14:52:02