Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    How to pass the event object when dynamically attaching an event handler in NS6

    Hi;
    I have a question regarding passing the event object in NS6 hopefully someone can clarify. I have a function that has a few parameters, one of which is the event object. I wish to dynamically assign this function to an event handler, which itself is attached to a HTML element using script. My question is, how do I pass the event object into this function in this case? The below should work, but it doesn't:

    Below code doesn't work in NS6
    Code:
    <form name="test">
    <input type="text" name="test2">
    </form>
    
    <script>
    function dothis(width,height,e){
    alert(e)
    }
    
    document.test.test2.onkeypress=function(e){ dothis(5, 10, event); }
    
    </script>
    The above returns an "event" not defined error. I have no problem getting things to work if I simply defined the event handler using HTML. For example, the below works:
    Code:
    <input type="text" name="test2" onkeypress="dothis(5,10,event)">
    Why is that?
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    function(e){

    A function with no name?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    anonymous functions are valid as of JavaScript 1.2. i use them all the time for event handlers. but generally not with the event object.

    George, the new event handling system still doesn't make sense to me, but from what i get out of the Guide you may have to do that this way:

    document.test.test2.onkeypress = function(e) { dothis(5, 10, e); }

    might also try it this way:

    document.test.test2.onkeypress=function() { dothis(5, 10, event); }
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    George, read the tutorial I submitted to your very own site.

    An object named "event" is passed as an argument to whatever function you assign the event to:

    Say when mousedown is fire on node "myNode", it does this:

    myNode.mousedown(event);

    (Essentially, though not really...)

    Which means:

    myNode.onmousedown = function(anynameyouwantfortheeventobject) {
    alert(thesamenameasbefore)
    }

    Or you can forget about naming it, and just call arguments[0].

  • #5
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks guys for the responses.I eventually got to where John is, which works in NS6:

    document.test.test2.onkeypress = function(e) { dothis(5, 10, e); }

    What still throws me off is why when you attach event handlers using HTML, you need to pass in "event", and not a name holder like e or "ewhateveryouwant", but in the above case, "e' instead in NS6. Specifically, I'm talking about "e" as it occurs here:

    dothis(5, 10, e);

    I guess my question really boils down to how parameters in anonymous functions operate.

    In IE4+, all this is much simplier, as there's simply "event" to deal with.
    Last edited by WA; 08-03-2002 at 04:51 AM.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Every time an event fires, the event object is a unique object. It makes more sense to me to pass this as an argument to the event listener every time, rather than pollute the global namespace with variables which really are better implemented elsewhere.

    BTW, this lets you create your own event objects and dispath them programatically without fear of altering the global event object via document.createEvent() and the various initEventType() methods.


  •  

    Posting Permissions

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