How to check if an enter key is pressed using jQuery
The “enter” key is represented by code “13″, check this ASCII charts.
To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox.
$('#textbox').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('You pressed a "enter" key in textbox');
}
});
To check if an enter key is pressed on-page, bind the keypress() to the jQuery $(document).
$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('You pressed a "enter" key in somewhere');
}
});
P.S In Firefox, you have to use event.which to get the keycode; while IE support both event.keyCode and event.which.
Get the name of the event that was fired in JQuery:
event.type

Post a Comment