How to grab keyboard events on an element which doesn't accept focus?

Viewed 27634

I know that for handling keyboard events in an input field you can use:

$('input').keyup(function(e){
var code = e.keyCode // and 13 is the keyCode for Enter
});

But, now, I have some div and li elements, and I don't have a form element, and none of my elements are considered to be form elements and none of them accept focus or tab and stuff like that.

But now I need to handle the keyup (or keydown, or keypress, doesn't matter) event in a div element. I tried:

$('div#modal').keyup(function(e){
   if (e.keyCode == 13)
   {
      $('#next').click(); // Mimicking mouse click to go to the next level.
   }
});

But the problem is, it doesn't work. What should I do?

3 Answers
Related