Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. WebView Android Full support Yes. We can manually reattach all event listeners to the cloned node but that sounds like a tedious task. Instead of fighting with attaching handlers to cloned nodes, attach a single event on the closest common parent element. Sign up using Facebook. Previously, the working group resolved to remove the related but less powerful hasEventListenerNS method for lack of a use case, and because there are potential security issues. Firefox Full support Yes. I cannot do Tim's option 1, to simply re-attach them during cloning as the messaging library is front-end framework agnostic.
Clone an element
a deep copy (optional argument) that contains the source element's. 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. › docs › Web › API › Node › cloneNode.
This is how jQuery's clone method is able to copy a node with its event listeners, for example.
If deep is set to falsechild nodes are not cloned. Name attributes may need to be modified also, depending on whether duplicate names are expected. Featured on Meta. I am re-skining an application in Semantic UI, which requires a small piece of JS to make the message close buttons work.
|RoelofCoertze - you would have to know which events listeners were added in the first place and also have access in the code's place where you've done the cloning, to assign the callbacks again, since accessing the callbacks might be tricky depending where the cloning took place in the code, so an example will be useless since there are too many scenarios.
Interestingly my previous front-end was in Zurb Foundation which uses a "data-closable" attribute, the functionality of which does survive the cloning process. Ask Question. This is what JeromeJ was describing in a comment.
Cloning a node copies all of its attributes and their values, including intrinsic in—line listeners. Chrome Full support Yes. If you are a performance freak — jsPerf shows a slightly faster computation for importNode.
Description: Create a deep copy of the set of matched elements. A Boolean indicating whether event handlers and data should be copied along with the elements. the discussion (), normally when an element is inserted somewhere in the DOM. Parameter, Type, Description.
deep, Boolean, Optional. Specifies whether all descendants of the node should be cloned. true - Clone the node, its attributes, and.
I thought I would add a concrete example, although it's in JQuery not Dojo.
Cloning DOM nodes and handling attached events
The newsletter is offered in English only at the moment. I cannot do Tim's option 1, to simply re-attach them during cloning as the messaging library is front-end framework agnostic. You cannot use it in your scripts.
Create the initial element using this HTML code.
HTML DOM cloneNode Method
IE Full support Yes.
|Chrome Android Full support Yes.
Feedback post: Moderator review and reinstatement processes. Hot Network Questions. I constantly look to improve it and work hard to drop some new features in every once in a while. For empty nodes e. This behavior has been changed in the latest spec, and if omitted, the method will act as if the value of deep was false.
It does not copy event listeners added using addEventListener or those assigned to element properties e.
Name attributes may need to be modified also, depending on whether duplicate names are expected. You cannot use it in your scripts. Hopefully that made sense and this article helped you out.
If the original node has an ID and the clone is to be placed in the same document, the ID of the clone should be modified to be unique. To create a shallow clone, deep must be set to false.
MURPHY MONJACK DEAD CONSPIRACY THEORISTS
|I thought I would add a concrete example, although it's in JQuery not Dojo.
RoelofCoertze - you would have to know which events listeners were added in the first place and also have access in the code's place where you've done the cloning, to assign the callbacks again, since accessing the callbacks might be tricky depending where the cloning took place in the code, so an example will be useless since there are too many scenarios.
It creates something commonly known as a shallow copy. Opera Android Full support Yes. The Node. Currently faced with this issue.