Skip to content

Instantly share code, notes, and snippets.

@ztrayner
Created March 16, 2018 16:38
Show Gist options
  • Save ztrayner/ac6d6bafeecd6cab09cf1bf0524b74cb to your computer and use it in GitHub Desktop.
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.
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