List events of an element wired up with addEventListener

M

mebemikeyc

With Element.addEventListener, you can easily wire-up several
different event handlers on the fly. This works great, but debugging
can be difficult if I don't know what exactly is reacting to an event.

Is there a way to list event listeners that have been wired-up to an
element's event?
 
T

Thomas 'PointedEars' Lahn

With Element.addEventListener, you can easily wire-up several
different event handlers on the fly. This works great, but debugging
can be difficult if I don't know what exactly is reacting to an event.

Is there a way to list event listeners that have been wired-up to an
element's event?

AFAIK only if you use a wrapper object, and use only its event
listener-registering methods to add event listeners to the wrapped
target object. For example:

function _EventListener(fListener, bCapture)
{
this.listener = fListener || null;
this.capture = !!bCapture;
}

function _EventRegistry()
{
this.data = {};
}

_EventRegistry.prototype.add = function(sEvent, fListener, bCapture) {
var d = this.data;

if (typeof d[sEvent] == "undefined")
{
d[sEvent] = [];
}

d[sEvent].push(new _EventListener(fListener, bCapture));
};

_EventRegistry.prototype.get = function(sEvent) {
return this.data[sEvent];
};

function _Element(oTarget)
{
this.target = oTarget;
this.events = new _EventRegistry();
}

_Element.prototype.addEventListener =
function(sEvent, fListener, bCapture) {
// add feature test here

this.target.addEventListener(sEvent, fListener, bCapture);
this.events.add(sEvent, fListener, bCapture);
};

// add feature test here

var o = new _Element(document.getElementById("foo"));
o.addEventListener("click", function() { window.alert(42); }, false);
window.alert(o.events.get("click")[0].listener);

BTW: `Element::addEventListener' is more like it. We are talking
*interfaces* here.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top