jQuery - detect enter key in jQuery

the following line of jQuery let's you detect when an enter key was pressed while focus was on a text input element.

Or you can use the live method to bind the keyup handler to future additions to the DOM:

$('input[type=text]').keyup(function(e) {
      if(e.keyCode == 13) {
 	     alert('Enter key detected with  jquery');
      }
});
$('.someClass').live('keyup',function(e){
	if(e.keyCode == 13) {
		alert('Enter key detected with  jquery');
	}
});
Reply:
 
 
 
 
Since the live method has been is deprecated in jQuery, nowadays the second example would be written something like this:
$(document).on('keypress', 'form', function(e){
	if (e.keyCode == 13) {
		e.preventDefault();
		alert('hiya');
	}
});
iugiugbo
rendered @ Mon May 20 4:22:07 CEST 2013