Remove '()
':
$this.on("keypress", keyAction);
You are adding undefined
as handler: $this.on("keypress", keyAction())
is equal to $this.on("keypress", undefined)
in your case, as keyAction
function does not return anything.
Also your div
must be focusable, in order to receive keyboard events. For that reason you need to add tabindex
on it:
<div class="dragClass" tabindex="0">
Then in selectAction
focus your div to receive keypress event:
$this.focus();
This is the DEMO
For more information about adding keyboard event
s on static elements such as div
, look at here: