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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onkeydown not triggered in firefox

    The following code works in IE but not Firefox. It appears that the event is not triggered in Firefox. Any solutions or work-arounds? Thanks in advance!

    -Charles

    Code:
    <HTML>
    <HEAD>
    <TITLE>test</TITLE>
    </HEAD>
    <BODY>
    <div onkeydown="alert('test');">
    <table border=1><tr><td>this is a test</td></tr></table>
    </div>
    </BODY>
    </HTML>

  • #2
    New Coder
    Join Date
    Feb 2006
    Location
    Italy
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This because IE is an idiot... you can't type into a div! The only elements that should trigger the "onkeydown" are input text, textarea and document
    Last edited by Davide Zanotti; 05-10-2006 at 05:47 PM.

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, according to this reference:
    http://www.w3schools.com/jsref/jsref_onkeydown.asp
    most of the tags support onkeydown, which may explain why Firefox doesn't throw up an error, so my question is how do I know the difference between "supporting" the event and "triggering" the event? I can't find a definitive reference for which tags actually trigger the event.

  • #4
    New Coder
    Join Date
    Feb 2006
    Location
    Italy
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cmcnulty
    Well, according to this reference:
    http://www.w3schools.com/jsref/jsref_onkeydown.asp
    most of the tags support onkeydown

    Maybe the idiot am I, but by using the logic... how could I type on a div,span,a or others similar elements?

  • #5
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Davide Zanotti
    Maybe the idiot am I, but by using the logic... how could I type on a div,span,a or others similar elements?
    Well that's exactly what happens in IE right now. If nothing is set to capture the event then nothing happens, but if you click somewhere in that area and an onkeydown event *is* defined, it allows you to capture those events.

    One example of taking advantage of this behavior would be allowing key navigation of a table. (clicking on a row highlights the row, pressing the down arrow highlights the row beneath it)

  • #6
    New to the CF scene
    Join Date
    May 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    By the way, thanks for the help so far Davide. Okay, so here's the work-around I'm considering. Because the div tag does support onclick, what I do is set a javascript flag such that when a user clicks inside the div tag it enables the document's onkeydown, and when the user clicks anywhere else on the page the onkeydown is disabled. Does this sound sensible? It would be soo much easier if Firefox just supported the keydown even inside div tags!

  • #7
    New Coder
    Join Date
    Feb 2006
    Location
    Italy
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could write something like this:

    myDiv.onclick=function(){
    document.onkeydown=function(){
    // do something
    }
    }

    but I still don't understand your aim :/

  • #8
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cmcnulty View Post
    Well, according to this reference:
    http://www.w3schools.com/jsref/jsref_onkeydown.asp
    most of the tags support onkeydown, which may explain why Firefox doesn't throw up an error, so my question is how do I know the difference between "supporting" the event and "triggering" the event? I can't find a definitive reference for which tags actually trigger the event.
    Firefox seems to need to have the tabindex="0" property set to some value so it knows this Div or Span is keyboard selectable. That allows the keyboard event to be triggered. It is not so dumb - you might want to trap Delete or Insert keys on a table etc.


  •  

    Posting Permissions

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