This question is what I ask, but with the restriction not to delete events for descendant nodes.
In this fiddle, I am trying to remove connected listeners by deleting it from the DOM and returning it.
function removeListeners(el) { var par = el.parentNode; var place = el.nextSibling; par.removeChild(el); par.insertBefore(el, place); }
Unfortunately, this did not work, you can still click to change the background (which is actually good, I never knew that you can attach events without an element located in the DOM).
Given this discovery, I tried this
function removeListeners(el) { var par = el.parentNode; var clone = el.cloneNode(false); par.replaceChild(clone, el); for (var index = 0; index < el.childNodes.length; ++index) clone.appendChild(el.childNodes[index]); }
Which tries to make a small clone, and then copies all the signatures, but does not copy all the child elements.
the above answer to the question said that "[if you want the children to keep their listeners], you will have to resort to explicitly removing the listeners one at a time." I do not know how you implement this.
I assume that it means getting a list of event types and removing listeners for each in turn ( as jQuery can ). Not only is this not possible for custom events , but vanilla JS has no such functions (you must specify a function to delete).
source share