How to remove all listeners from the page?

I know how to remove a listener from an element, but how can I remove every event listener from each element on a page?

It would be nice to use jQuery and a pure JS solution.

+4
source share
4 answers

I would suggest a peek at disabling the jQuery function . Also, based on fooobar.com/questions/201012 / ... , I would try to remove all listeners with the following code

$("body").find("*").off();

Hope this helps you.

+2
source

You can also try this.

http://jsfiddle.net/LkfLezgd/9/

$("#cloneButton").click(function() {
$('body').replaceWith($('body').clone().html());
});
+1
source

, , .

The quickest way to do this is to simply clone the node, which will remove all event listeners, but it will not be deleted if the 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>
Run codeHide result
+1
source

Just Clone HTML dom, which will delete events by default.

Here is an example.

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#EventButton").click(function () {
                alert("Click event worked");
                var old_element = document.documentElement;
                var new_element = old_element.cloneNode(true);
                old_element.parentNode.replaceChild(new_element, old_element);
            });
        });
    </script>
</head>

<body>
    <button id="EventButton">Remove Events</button>
</body>

</html>
Run codeHide result
+1
source

Source: https://habr.com/ru/post/1673855/


All Articles