If you add an event with addEventListener()
you must have a reference to that function available to be able to subsequently remove it.
With an anonymous function that's only possible with arguments.callee
, and then only while you're within the function itself:
element.addEventListener('click', function() {
this.style.backgroundColor = '#cc0000';
this.removeEventListener('click', arguments.callee);
}, false);
but note that this isn't legal in ES5 "strict mode".
Hence it would just be better to give your callback a name and then use that in the call to removeEventLister()
:
element.addEventListener('click', function cb() {
this.style.backgroundColor = '#cc0000';
this.removeEventListener('click', cb);
}, false);
Demo at http://jsfiddle.net/alnitak/RsaVE/