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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using anonymous function in onkeydown/up to send arguments, how?

    Hi,

    this code doesn't have any problem in IE, however, it doesn't work in Mozilla and it doesn't give me any error message.

    Code:
    <body>
    <input id="damn" type="Text">
    <script>
    
    document.getElementById("damn").onkeydown = function()
    											{
    												keydown( event, this );
    											}
    document.getElementById("damn").onkeyup = function()
    											{
    												keyup( event, this );
    											}
    function keyup( oEvent, element )
    {	
    	var key = oEvent.keyCode;
    	alert(key);
    }
    function keydown( oEvent, element )
    {
    	var key = oEvent.keyCode;
    	alert(key);
    }
    </script>
    As you see, I'm only trying to send some arguments to my functions, but because the syntax doesn't let me do it directly, I have to use an anonymous function. Is there any way I can fix it? I can't use onkeydown/up as the input's attributes directly in the HTML.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    IE uses the global Event object, NN4, Moz and some others pass the event as the first argument of the function instead.
    Code:
    document.getElementById("damn").onkeydown=function(e){
        keyevent(e||event,this);
    }
    
    document.getElementById("damn").onkeyup=function(e){
        keyevent(e||event,this);
    }
    
    function keyevent(oEvent,element){	
    	alert(oEvent.type+': '+oEvent.keyCode);
    }
    Last edited by liorean; 04-09-2003 at 04:10 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    shouldn't both anonymous functions accept the same arguments?

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yes, they should. I guess I forgot to add it to the other one. It's corrected now.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whew! I thought I was coming apart at the seams. I'm still pretty rough with javascript; I hope you didn't take any offense re: my remark. I just read it and realized it sounded a little hurried and harsh.

    I've always liked this kind of approach (associating elements with handlers in the script itself, outside the markup), but why not use the attachEvent() method? What's the target browsing environment?

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    attachEvent is IE proprietary. W3C uses addEventListener. Most browsers only support window.onevent.
    Last edited by liorean; 04-09-2003 at 07:13 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh. Yeah. addEventListener.

    *thwack self in head*

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Damet garm! Kheili bahali!

    The above sentence is like yelling: YOU ARE COOOOOOOOOL in my language! I'm writing this easyFarsi function, which turns any input/textarea into a Farsi editor. I'm about to finish it and you know when those little problems come up! Just thanks for your help.

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I'm here to serve - I personally seldom need the help of these forums. They are interesting, though. Beetle, Brothercake, JKD, and a few others as well, are really good sources for nifty tricks, inspiration, and commentary.

    In fact, I never had a good look into the ways event handlers worked before you asked a few event handler questions, so I guess you helped me learn something as well.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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