Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind keyboard events to div elements?

Is there a way to listen to keyboard events on a DIV element?

My code:

​<div id="div" style="height:50px" class="ui-widget-content"></div>
<input id="input">​​​​​​​​​​​​​​

​$('#div,#input').keyup(function(event){
    console.log(event.keyCode);
});​​​​​​

Actually, the code triggers only for the input, can I handle it for the div?

like image 923
ilyes kooli Avatar asked May 23 '12 14:05

ilyes kooli


2 Answers

You can add a tabindex in that div to catch keyboard events like this

<div id="div" style="height:50px" class="ui-widget-content" tabindex="0"></div>

Like answered here.

Working Fiddle

Reference

like image 157
Prasenjit Kumar Nag Avatar answered Nov 06 '22 12:11

Prasenjit Kumar Nag


Add a tabindex and it should work

<div id="div" style="height:50px;" class="ui-widget-content" tabindex="1"></div>

DEMO

like image 5
Selvakumar Arumugam Avatar answered Nov 06 '22 13:11

Selvakumar Arumugam