0

Normally to get listeners on that DOM node I am using

$('selector').data('events');

However this does not show event listeners that are being add via delegation, e.g

$(document).on('click', 'selector', handlerFunction) 

One obvious way is to traverse up the DOM tree and look if any of parents are delegating events to element at hand, by concurrently calling $('selector').parent().data('events') until no parent can be found, however this does not strike me as very efficient or standard way of doing things, and I think of it this sort of problem is too common not to have a better solution.

How to find all the event listeners including delegated ones?

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
  • hmm.. what are you actually trying to do..? this might be an xy problem... – T J Jul 14 '14 at 10:05
  • I have massive pages with tons of even listeners being added from all sorts of modules and other crap (like Jquery mobile ui) and other horrors firing and triggering things all around the place, and I am trying to make sense out of it. I don't think is 'xy' unless one can read code like Neo reads matrix which I frankly cannot :). – Matas Vaitkevicius Jul 14 '14 at 10:18
  • Traversing the DOM up is the only way to get delegate listeners, that is because those listeners don't belong to the elements matching `'selector'` but to the the element `$(element)`. So even if jQuery would provide such a function it would need to do the traversing itself internally. – t.niese Jul 14 '14 at 10:19
  • @t.niese thanks, I have thought so myself, just needed confirmation that there's no known 'out of the box' solution. – Matas Vaitkevicius Jul 14 '14 at 10:24
  • So you need to know this for debugging purpose? (Delegate event listeners are a nice thing, but they are often used thoughtless. Especially if bad written plugins in single page application pollute the `document` element with delegate listener.) – t.niese Jul 14 '14 at 10:50
  • Event if I don't think that you can get a better solution for one particular element you maybe could look at [VisualEvent](https://github.com/DataTables/VisualEvent), which visualizes all currently attached/matching event listeners. – t.niese Jul 14 '14 at 11:01

1 Answers1

0

ATM I am using functions below, not to elegant - but saves me quite some time.

var getAllEventListeners = function (options) {
    if (options.internalArr == undefined)
        options.internalArr = [];
    if (options.elements.data('events') != undefined) {
        options.internalArr.push({
            elements: options.elements,
            events: options.elements.data('events')
        });
    }
    if (options.elements.parent().length != 0) {
        getAllEventListeners({
            elements: options.elements.parent(),
            internalArr: options.internalArr
        });
    }
}

var findAllListeners = function (selector) {
    var opt = {
        elements: $(selector),
        internalArr: []
    };
    getAllEventListeners(opt);
    return opt.internalArr;
}
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265