Skip to content
Advertisement

How to let a div or span tag to receive a keydown event in Javascript

For example,

<div onkeydown='alert(3)' style="background:yellow;height:300px;width:300px;">
Click at me first. and press a key to see the event
</div>

Here is a jsfiddle example

Advertisement

Answer

You can specify the tabindex on it.

For example,

<div onkeydown='alert(3)' tabindex="0" style="background:yellow;height:300px;width:300px;">
Click at me first. and press a key to see the event
</div>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement