Just question: Is there any way to completely remove all events of an object, e.g. a div?
EDIT: I'm adding per div.addEventListener('click',eventReturner(),false);
an event.
function eventReturner() {
return function() {
dosomething();
};
}
EDIT2: I found a way, which is working, but not possible to use for my case:
var returnedFunction;
function addit() {
var div = document.getElementById('div');
returnedFunction = eventReturner();
div.addEventListener('click',returnedFunction,false); //You HAVE to take here a var and not the direct call to eventReturner(), because the function address must be the same, and it would change, if the function was called again.
}
function removeit() {
var div = document.getElementById('div');
div.removeEventListener('click',returnedFunction,false);
}
May be the browser will do it for you if you do something like:
Copy the
div
and its attributes and insert it before the old one, then move the content from the old to the new and delete the old?As corwin.amber says, there are differences between Webkit an others.
In Chrome:
Which gives you an Object with all the existing event listeners:
From here you can reach the listener you want to remove:
So All the event listeners:
In Firefox
Is a little bit different because it uses a listener wrapper that contains no remove function. You have to get the listener you want to remove:
All the event listeners:
I stumbled with this post trying to disable the annoying copy protection of a news website.
Enjoy!
//edit
I didn't knew what method are you using for attaching events. For
addEventListener
you can use this:more details
Use the event listener's own function
remove()
. For example:This will remove all listeners from children but will be slow for large pages. Brutally simple to write.
Removing all the events on
document
:One liner:
Pretty version: