How to check if the enter key is pressed using jQuery

Here’s a tiny code snippet which allows you to detect if the enter key was pressed using jQuery. To do this, we are going to use ASCII key code values.

Click Here to view the working demo.

HTML:

<input type="text" id="inputbox" name="inputbox" />

jQuery:

$(document).ready(function(){
	$('#inputbox').on('keypress',function(){
		var keyCode = (event.keyCode ? event.keyCode : event.which);
		if(keyCode == '13'){
			alert('Enter key is pressed!');
		}
	})
})

See Also: Disable right click using jquery

0 Comments

Leave a Comment.