If you add an event with addEventListener() , you must have a link to this function in order to be able to delete it later.
With an anonymous function, which is only possible with arguments.callee , and then only while you are inside the function itself:
element.addEventListener('click', function() { this.style.backgroundColor = '#cc0000'; this.removeEventListener('click', arguments.callee); }, false);
but note that this is not legal in ES5 "strict mode".
Therefore, it would be better to give your callback a name and then use it in the removeEventLister() call:
element.addEventListener('click', function cb() { this.style.backgroundColor = '#cc0000'; this.removeEventListener('click', cb); }, false);
Demo at http://jsfiddle.net/alnitak/RsaVE/
source share