I have a page where I need to capture input from the user after they click on parts of the page (div). Here is my code:
<html> <body> <div style="background-color: lightpink" onkeydown="onkeydown1(event)" tabindex="-1"> click me, then press a key </div> <script type="text/javascript"> function onkeydown1(event) { alert(event.charCode); } </script> </body> </html>
See in action: http://jsfiddle.net/WRwBF/
It took me the longest time to go that far, because FireFox does not allow the div to “have focus” by default. In the end, I found out that setting tabindex for the div allows it to be in focus, and the onkeydown event works.
Now my problem is that when I click on the div and press the key, the value "0" is returned no matter which key is pressed. Why is this happening, and how can I fix it?
I would really appreciate any advice you could give!
source share