2

I have a difficult question.

events = $._data( element[0], 'events'); 

$.each(events, function(_event_name, _event_handler){ 
    var _handlers=[]; 
    for(var i= 0 ;i < _event_handler.length;i ++) 
            _handlers.push(_event_handler[i].handler);
    event_handler.push(_handlers); 
    event_name.push(_event_name); 
}); 

element.off(); 

I have above code to successfully read all what event name and its handler assigned for a element.

Then I save each of them into event_name and event_handler before I turn the events off;

However, this method is only work on when the events are directly assigned for the element.

When the events are delegated assigned, how can I develop the code to do that?

$(document).on('click', '#id', handler);

Above code will only show the event name click and its handler handler, but no the name or selector of the delegate assigned element #id.

I want to know how can I read the name of delegate assigned element out that I can do delegate off or all the events on parent will be off.

Thank you very much for your advice.

Micah
  • 4,254
  • 8
  • 30
  • 38

1 Answers1

3

The selector is stored in the object within the array of click events under selector as seen below.

It seems that the selector field is empty when you look at click events bound directly to the element.

I would assume you can build a check into your code somehow. Mind you, the selector property is apparently only intended for internal use as I had a question myself in the past about it, when I found it contained occasionally strange values.

To that end, though it might work for you, I do not know how reliable it is or even if it is going to be used in future versions of jQuery.

However, if you only use it for delegate events you might find it contains useful values but keep an eye on it.

enter image description here

Nope
  • 22,147
  • 7
  • 47
  • 72
  • Thank you very much, let me check of it – Micah Feb 09 '13 at 23:33
  • @Till: That is the only way I could think off getting to the selector for the delegate event. When I asked my own question in the past regarding the `selector` property as I used it and found sometimes it has strange formatted values. I was told that it is a property which, though accessible, is intended for internal use only. The question was [**here**](http://stackoverflow.com/questions/12426622/why-does-the-selector-property-in-jquery-not-store-a-valid-selector-value) in case it has information in it you find useful. – Nope Feb 09 '13 at 23:41
  • @Till: In addition I just noticed that delegate vents have the `quick` array property populated while direct events do not. In the answer you can see the `quick` array contains 4 items. `quick[0]` has a value of `#id`, `quick[1]` has `""`, `quick[2]` has "id" and `quick[3]` is undefined. You might be able to use that as well somehow. – Nope Feb 09 '13 at 23:53