Sometimes we need to detect where ENTER key is pressed or not to do some action.
The "enter" key is represent by code 13 (ASCII value).
To detect whether ENTER key is pressed on Web-page or inside an input field, we can bind keypress() event to that input field (textbox) or the document object.
Example:
Check "enter" key is pressed inside an input field:
<input id="search-box" type="text"/>
$('#search-box').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('Enter key pressed inside input field.');
}
});
Check "enter" key is pressed on Web page:
$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('Enter key presses in somewhere.');
}
});
Hope this will help you:)
0 Comment(s)