Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  169] [ 2]  / answers: 1 / hits: 23671  / 12 Years ago, wed, may 23, 2012, 12:00:00

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?


More From » jquery

 Answers
6

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


[#85392] Tuesday, May 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
calicinthias questions
Sun, Jan 2, 22, 00:00, 2 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Mon, Aug 10, 20, 00:00, 4 Years ago
;