Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  106] [ 6]  / answers: 1 / hits: 53722  / 11 Years ago, thu, march 14, 2013, 12:00:00

I tried



node.cloneNode(true); // deep copy


It doesn't seem to copy the event listeners that I added using node.addEventListener(click, someFunc);.



We use the Dojo library.


More From » dom

 Answers
31

cloneNode() does not copy event listeners. In fact, there's no way of getting hold of event listeners via the DOM once they've been attached, so your options are:




  • Add all the event listeners manually to your cloned node

  • Refactor your code to use event delegation so that all event handlers are attached to a node that contains both the original and the clone

  • Use a wrapper function around Node.addEventListener() to keep track of listeners added to each node. This is how jQuery's clone() method is able to copy a node with its event listeners, for example.


[#79599] Wednesday, March 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;