Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger an onkeydown event on html table on Firefox?

I want to capture the keystrokes on a table's cells. I have the following code that is working on IE but not on Firefox/Chrome.

<table id="testing" onkeydown="alert('testing')"><br />
    <tr><td>testing</td></tr>` <br />
    </table>
<br />

Any suggestion?

like image 881
Stavros Avatar asked May 20 '09 12:05

Stavros


2 Answers

http://www.w3schools.com/jsref/jsref_onkeydown.asp specifies that most of the tags support onkeydown.

You need to set tabindex property (eg tabindex="1") to some value, so it can identify that this table is keyboard selectable. That allows the keyboard event to be triggered. Following will work in firefox:

<table id="testing" onkeydown="alert('testing')" tabindex="0"><br />
<tr><td>testing</td></tr><br />
</table> 
like image 102
Sourabh Avatar answered Oct 25 '22 14:10

Sourabh


This appears to be an issue with Firefox and Chrome's interpretation of the specifications rather than your code. I just tried this myself and reproduced the issue.

The W3C specifications show that Table cells should support onkeydown but this doesn't appear to be supported by Firefox/Chrome currently, you could try reporting this as a Firefox bug through their website. You usually get some feedback as to why it's broken (or why they think it's not broken and won't fix it as I've had in the past)

Can I ask why you need to capture keystrokes on table cells?

  • If you are expecting people to enter text then you should use <input> tags which will register key press events fine.
  • If the keystrokes are some kind of navigation then perhaps a context menu done based on mouse click events would be better
like image 24
RobV Avatar answered Oct 25 '22 15:10

RobV