Results 1 to 7 of 7

Thread: removeEventListener();

  1. #1
    Join Date
    May 2007
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default removeEventListener();

    i have the following code...

    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...

    Code:
    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 =)

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    to remove the event listener use: removeEventListener('click',arguments.callee,false);
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    May 2007
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    thanks for the response... but it's not working. i did this...

    Code:
    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?

  4. #4
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    then try arguments.callee.caller
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  5. #5
    Join Date
    May 2007
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    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?

  6. #6
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    Okay then.
    Code:
    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);
    }
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  7. The Following User Says Thank You to Master_script_maker For This Useful Post:

    ahs10 (04-21-2009)

  8. #7
    Join Date
    May 2007
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •