SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict y_oda2002's Avatar
    Join Date
    May 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    addEventListener parameters

    Hi Everyone,

    Is there a way to send parameters to the function being added to an event with addEventListener.

    I.E.

    say you have this function
    Code:
    function someFcn(i){
     alert(i);
    }
    and I add it to an object.
    Code:
    someElement.addEventListener('focus', someFcn, false);
    Is there a way to send a parameter to someFcn.
    For Example I have tried this but it failed
    Code:
    var someString = 'Hello World';
    someElement.addEventListener('focus', someFcn(someString), false);
    Thanks for any help,

    Nick
    Nicholas Rhodes
    Asgard Development
    http://www.asgard-dev.com

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That will not work unless someString is a variable in the global scope. You have to do this:

    Code:
    var someString = 'Hello World';
    someElement.addEventListener('focus', function() {
      someFcn(someString);
    }, false);

  3. #3
    Non-Member
    Join Date
    Aug 2006
    Location
    Kansas City
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Raffles,

    I would like to add to this thread.
    Is there a fundemental difference between:

    someElement.addEventListener('focus',function)

    and:

    someElement.attachEvent("onmouseover", function)

    Is the second one IE only?

    Thanks

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, the second one is IE only. The first one, addEventListener, should also have a third parameter (true or false), which specifies whether to add the listener in the bubbling or capturing phase. This is a good page to read about these two: http://www.quirksmode.org/js/events_advanced.html


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
  •