I know how to remove a listener from an element, but how can I remove every event listener from every element on the page?
A jQuery and pure JS solution would be nice.
I would suggest to look into the .off function of jQuery. Also, based on this stackoverflow question, I would try to remove all every listeners with the following code
$("body").find("*").off();
Hope this could help you.
You can try this too.
http://jsfiddle.net/LkfLezgd/9/
$("#cloneButton").click(function() {
$('body').replaceWith($('body').clone().html());
});
You can loop through the form elements as below to remove the event listeners at your preferred way.
The fastest way to do this is to just clone the node which will remove all event listeners but this won't remove if 'onclick' attribute is used.
document.getElementById('button').addEventListener('click', onClick, false);
function onClick() {
console.log('Form clicked');
var form = document.getElementById('myForm');
for(var i = 0; i < form.elements.length; i++){
var elm = form.elements[i];
removeEvents(elm);
}
}
function removeEvents(elm) {
// The fastest way to do this is to just clone the node, which will remove all event listeners:
var new_element = elm.cloneNode(true);
elm.parentNode.replaceChild(new_element, elm);
}
<form id="myForm">
<input id="button" type="button" value="Click" />
</form>