Created
March 16, 2018 16:38
-
-
Save ztrayner/ac6d6bafeecd6cab09cf1bf0524b74cb to your computer and use it in GitHub Desktop.
This function clones an element/node and thus removes all event listeners. Using the returned promise, you can then set new listeners on the element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cloneNodeRemoveEvents(el, withChildren) { | |
if (withChildren) { | |
el.parentNode.replaceChild(el.cloneNode(true), el); | |
} | |
else { | |
var newEl = el.cloneNode(false); | |
while (el.hasChildNodes()) newEl.appendChild(el.firstChild); | |
el.parentNode.replaceChild(newEl, el); | |
} | |
return new Promise(function(resolve, reject) { | |
resolve(newEl); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment