Not able to track enter key on input type = text
Can anybody tell me why is this not working?
开发者_StackOverflow社区 <input name ="txtChat" id ="txtChat" onkeydown ="isEnterPressed();" type="text" style="width:720px;"/>
This is my javascript function :-
function isEnterPressed(event){
alert(event);
}
On alert i always gets undefined :(
Thanks in advance :)
There is no built-in event type that is fired when the enter key is pressed, you need to check that in the Event
object that is passed to the handler.
HTML:
<input name ="txtChat" id ="txtChat" onkeyup="onKeyPressed(event)" type="text" style="width:720px;"/>
JavaScript:
function onKeyPressed(ev) {
var e = ev || event;
if(e.keyCode == 13) {
//Enter was pressed
return false; //prevents form from being submitted.
}
}
A very simple inline check is:
<input name ="txtChat" id ="txtChat" onkeydown ="alert(event.keyCode == 13);" type="text" style="width:720px;"/>
which outputs true
if enter is pressed.
精彩评论