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


Groups > comp.lang.javascript > #18051

Re: Clone node with events

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!.POSTED!not-for-mail
From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Clone node with events
Date Thu, 10 Jan 2013 17:15:44 +0100
Organization albasani.net
Lines 27
Message-ID <kcmpfg$pli$1@news.albasani.net> (permalink)
References <kcm8um$8pi$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace news.albasani.net hhxmPN1cDyjHfn9gk//73oemA298qq7Co+UsFthTiZsim2nJfktOoRBpoezqqOZtrJh9HM4M1UXan2IZ2zK5NaAiZ7vI2BdLg7A/qne9gDoy9j+zYPGogLHJgkLAD0pk
NNTP-Posting-Date Thu, 10 Jan 2013 16:15:44 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="HL3LCTXyU15vSi4q3ELZOFYbzC61trYLb4e1nqGinwfZe6l+nWTFss5SQQ0fdzwl8cdM4/Mfw5CuKAaZL/mnIpSecrOFgYhXEag33x511/m173a9AZG/TWGTNq91b1Vt"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <kcm8um$8pi$1@speranza.aioe.org>
Cancel-Lock sha1:/b3jBahc9FhA2eLkDxwlKyFsgCQ=
Xref csiph.com comp.lang.javascript:18051

Show key headers only | View raw


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.

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;
   }

- stefan

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