Item Event List

Is there a way to find all events for an element in javascript? or a way to detach all events from an element?

Thank.

+3
source share
3 answers

It is not possible to do this directly using javascript.

Jquery has several features to track this data.

One β€œclean” way to do this is to change the addEventListener / attachEvent function using prototyping (search for javascript prototype material, not the framework).

Or, if you have a custom function for adding / removing events, you can configure it.

Good thing it is.

+3
source

jQuery, ( ) - , 'on' (onclick ..),.

var el = document.getElementById('elementid') ;
el.onclick = function(e) { console.log('Clicked!') ; } ; // Attached test event.
if(typeof(el)=='object') {
   for(var i in el) {
      if(i.substr(0,2) == 'on' && typeof(el[i])=='function') {
         el[i] = function() {} ; // Unbind with null function.
      }
   }
}
+3

Of course! Take a look at this to bind / unbind the events of http://api.jquery.com/category/events/ and use this jQuery code snippet to get all events bound to an element in the form of a hash key set "eventname / function delegate"

jQuery(elem).data('events');
+2
source

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


All Articles