...

View Full Version : How to get all event Listeners



Mikebert4
08-08-2008, 11:39 AM
I'm attaching events to buttons on my page using the following code:


if(document.attachEvent)
{
theButton.attachEvent('onclick', function(e){button(e)});
}
else if(document.addEventListener)
{

theButton.addEventListener('click', function(e){button(e)}, false);
}
else
{
theButton.onclick = function(e){button(e)};
}


however, some of these elements already have events attached (up to 2 or 3).

Is there a way to access all the events attached to an element, and loop through them?

I'm thinking a method similar to this:



function removeEvents(element)
{
for(i=0; i<element.events.length; i++)
{
element.removeEventListener(events[i].type, events[i].function, false)
}
}


Of course, I just made all that last code up, but is there somthign of that ilk I can use?

shyam
08-08-2008, 07:50 PM
sadly, no :(

Arbitrator
08-09-2008, 06:50 PM
function removeEvents(element)
{
for(i=0; i<element.events.length; i++)
{
element.removeEventListener(events[i].type, events[i].function, false)
}
}


Of course, I just made all that last code up, but is there somthign of that ilk I can use?If your intent is to simply remove all event listeners from an element, you can do that by using cloneNode; DOM2 Events event listeners are not copied when cloneNode is used.


var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);
new_element.addEventListener("click", function_reference, false);

I believe that attachEvent persists even when an element is cloned though, so Iím not sure what you would do for Internet Explorer users.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum