Adam Halasz
Adam Halasz

Reputation: 58311

Javascript : RemoveEventListener with Anonym Function

How to remove and event listener with an Anonym function , with removeEventListener();

document.getElementById("object").onclick = function(e){
    if(e && e.stopPropagation) {
        e.stopPropagation();
    } else {
          e = window.event;
          e.cancelBubble = true;
    }
}

So I have this piece of code and the function what's called must be anonym I'dont know why but If it's not then doesn't works correctly, maybe beacuse of the event :|

But If it's anonym how can I remove it?

Upvotes: 2

Views: 581

Answers (2)

Cipi
Cipi

Reputation: 11353

Just give it a null value, which is the starting value when onclick is not initialized: document.getElementById("object").onclick = null

Upvotes: 2

Olical
Olical

Reputation: 41372

Well you havent added an actual event listener, you have just populated the onclick variable with a function to be run. So you should be able to just use something like this:

document.getElementById("object").onclick = false;

EDIT

Just tried it in jsFiddle and what I suggested works.

Upvotes: 3

Related Questions