Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18095

Re: Clone node with events

From Cezary Tomczyk <cezary.tomczyk@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Clone node with events
Date 2013-01-12 23:00 +0100
Organization Aioe.org NNTP Server
Message-ID <kcsme8$8of$2@speranza.aioe.org> (permalink)
References <kcm8um$8pi$1@speranza.aioe.org> <kcmpfg$pli$1@news.albasani.net>

Show all headers | View raw


W dniu 2013-01-10 17:15, Stefan Weiss pisze:
> On 2013-01-10 12:33, Cezary Tomczyk wrote:
>> Any idea how to in an efficient and proper way clone node with events?
>
> That doesn't seem like a good idea in the first place (for the reason
> Scott mentioned), but maybe there's a better way. When you have a number
> of elements that should react in a similar way to an event, consider
> using event delegation instead of separate listeners for each element.

Yes, I know event delegation. They are powerful :-)

> Simply put, that means attaching one listener to a common parent
> element, and resolving the event target from there. This works
> especially well for click events; other event types (like mouseover,
> mouseout) can be a little more tricky. Some events do not bubble at all
> (focus, blur, change, etc).
>
> If you can't use event delegation, use a wrapper function to
> create/clone the element and attach the listeners at the same time:
>
>     function handleFocus (evt) {}
>
>     function makeInput (node) {
>         var input = node ? node.cloneNode()
>                          : document.createElement("input");
>         input.addEventListener("focus", handleFocus);
>         return input;
>     }

That is the only way, as I see, at this moment. But this is useful when 
you know what type of events are attached. Otherwise, as I mentioned in 
reply to Scott, it is better to write some wrapper to event methods and 
use collected data (I mean attached events) to attach them again after 
replacing element.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-10 12:33 +0100
  Re: Clone node with events Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-10 07:30 -0800
    Re: Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-12 22:57 +0100
      Re: Clone node with events Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-13 15:07 +0100
        Re: Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-16 20:50 +0100
  Re: Clone node with events Stefan Weiss <krewecherl@gmail.com> - 2013-01-10 17:15 +0100
    Re: Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-12 23:00 +0100
  Re: Clone node with events RobG <rgqld@iinet.net.au> - 2013-01-13 22:07 -0800

csiph-web