cmcnulty
05-10-2006, 04:34 PM
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
<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>
Davide Zanotti
05-10-2006, 04:45 PM
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
cmcnulty
05-10-2006, 05:04 PM
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.
Davide Zanotti
05-10-2006, 05:09 PM
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?
cmcnulty
05-10-2006, 05:13 PM
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)
cmcnulty
05-10-2006, 10:26 PM
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!
Davide Zanotti
05-11-2006, 01:06 PM
You could write something like this:
myDiv.onclick=function(){
document.onkeydown=function(){
// do something
}
}
but I still don't understand your aim :/
docrichards
10-31-2006, 10:44 AM
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.