Cezary Tomczyk wrote:
> […]
> consider this simple example:
>
>
http://jsfiddle.net/JpLx2/
>
> All I need here is to just copy attached events from old_node to
> new_node, but seems that is not so easy to do. This could be possible
> done by, for example, adding some wrapper to event methods and then use
> it.
First of all, you are _not_ copying events. You could be copying event
*listeners*, but all you would copy in ECMAScript would be *references* to
Function instances.
So there is no problem here. You have a reference to a Function instance,
and the “click” event bubbles in all know DOM implementations. So, as
already suggested, you should use event delegation, in particular use event
bubbling: Add to a common ancestor element a listener for that event that
does actions only for specific event targets. When a cloned target receives
that event, it has no listener for it; since the event bubbles, it will be
handled by the ancestor element which has a listener for it.
BTW, you should not be using attachEvent() as an alternative to
addEventListener():
<
http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html>
> Events-20010823/events.html#Events-EventListenerList,
> but seems that it is not implemented, for example, in Firefox 17.0.1 and
> Opera 12.12. Probably in other browsers also is not implemented :-(
Neither is it implemented in WebKit WebCore as of Chromium 22. You are
referring to an *old* Working Draft. The *current* Working Draft for W3C
DOM Level 3 Events is available at
<
http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120906/>.
You will notice that the EventTarget interface no longer has an
“eventListeners” attribute of type “EventListenerList”, and that the
“EventListenerList” interface has been removed:
<
http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120906/#interface-
EventTarget>
And it would seem that no replacement for that attribute or interface has
been suggested yet.
However, with proprietary event handler properties (called “traditional
event registration” on QuirksMode.org) now perhaps going to be a Web
standard with the HTML5 API, for elements and events for which only one
event listener has been added, you could use those properties to retrieve
the primary event listener for an event on an element:
<
http://www.w3.org/html/wg/drafts/html/master/index.html#ix-event-handlers>
<
http://www.w3.org/html/wg/drafts/html/master/webappapis.html#event-
handlers>
While that has worked even before HTML5 (jsx.dom.*EventListener() are based
on it), I recommend you use event bubbling here.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.