PDA

View Full Version : removeEventListener();



ahs10
04-20-2009, 06:54 PM
i have the following code...



for (i=0; i<5; i++) {
var t = document.getElementById('div'+i);
t.addEventListener('mouseover',function () {adminStyle(this.id,'over');},false);
t.addEventListener('mouseout',function () {adminStyle(this.id,'out');},false);
t.addEventListener('dblclick',function () {lightWindow('linkDemo');},false);
}


i used an anonymous function because i was having trouble passing values to the name functions. now that i'm using anonymous function though, i don't know how to remove the event listener. this doesn't work...



for (i=0; i<5; i++) {
var t = document.getElementById('div'+i);
t.removeEventListener('mouseover',function () {adminStyle(this.id,'over');},false);
t.removeEventListener('mouseout',function () {adminStyle(this.id,'out');},false);
t.removeEventListener('dblclick',function () {lightWindow('linkDemo');},false);
}


i believe in order to solve my issue i need to know one of two things...

1 - how do i addEventListener and pass variables to the function that is to be called on the specified event?

or

2 - how do i removeEventListener if it's an anonymous function?

got the time, and i'd love the answer to both. if not, just one will do fine, thanks for your time =)

Master_script_maker
04-20-2009, 07:01 PM
to remove the event listener use: removeEventListener('click',arguments.callee,false);

ahs10
04-20-2009, 07:12 PM
thanks for the response... but it's not working. i did this...



for (i=0; i<5; i++) {
var t = document.getElementById('div'+i);
t.removeEventListener('mouseover',arguments.callee,false);
t.removeEventListener('mouseout',arguments.callee,false);
t.removeEventListener('dblclick',arguments.callee,false);
}


after that loop runs, the events still exist. i put alert(i); after the line that declares the "t" var, and it gave me a pop up 5 times, as expected. what am i doing wrong?

Master_script_maker
04-20-2009, 07:25 PM
then try arguments.callee.caller

ahs10
04-20-2009, 08:06 PM
nope, no go there either. "arguments.callee.caller" does not work

i did find another solution similar to mine, but i'm trying to find a way to make it work in a incremental loop like this.



document.addEventListener('mousemove', afunc = function(e){GM_log(e)}, true)
document.removeEventListener('mousemove', afunc, true)


it seems that you can name the anonymous function within addEventListener itself. i need to figure out how to get a unique name for each of the 3 events, for each increment of the loop, but then i should be good to go, right?

Master_script_maker
04-20-2009, 09:53 PM
Okay then.

var over=function () {adminStyle(this.id,'over');};
var out=function () {adminStyle(this.id,'out');};
var click=function () {lightWindow('linkDemo');};

for (i=0; i<5; i++) {
var t = document.getElementById('div'+i);
t.addEventListener('mouseover',over,false);
t.addEventListener('mouseout',out,false);
t.addEventListener('dblclick',click,false);
}
for (i=0; i<5; i++) {
var t = document.getElementById('div'+i);
t.removeEventListener('mouseover',over,false);
t.removeEventListener('mouseout',out,false);
t.removeEventListener('dblclick',click,false);
}

ahs10
04-21-2009, 05:06 PM
perfect, works like a charm. thanks for your time and thought on this =)

i am curious however, why doesn't your solutions using arguments.callee.caller or arguments.callee work? i read up on them, and by definition it seems like that should do the trick. any ideas on what's preventing that from working?