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.

Try it yourself

<html>
<head>
 
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
 
</head>
<body>
  <h1>Check if "enter" is pressed with jQuery</h1>
 
<label>TextBox : </label>
<input id="textbox" type="text" size="50" />
 
<script type="text/javascript">
 
$('#textbox').keypress(function(event){
 
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in textbox');	
	}
	event.stopPropagation();
});
 
$(document).keypress(function(event){
 
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in somewhere');	
	}
 
});
 
</script>
</body>
</html>
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at jQuery Tutorials