The “enter” key is represent 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.

Ref:- http://www.mkyong.com/jquery/how-to-check-if-an-enter-key-is-pressed-with-jquery/

Get the name of the event that was fired in JQuery:

event.type

Advertisements