The easy way to detect whether the user has pressed enter is to use key number the enter key number is =13 to check the value of key in your device
$("input").keypress(function (e) {
if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25)
|| (97 <= e.which && e.which <= 97 + 25)) {
var c = String.fromCharCode(e.which);
$("p").append($("<span/>"))
.children(":last")
.append(document.createTextNode(c));
} else if (e.which == 8) {
// backspace in IE only be on keydown
$("p").children(":last").remove();
}
$("div").text(e.which);
});
by pressing the enter key you will get result as 13 . using the key value you can call a function or do whatever you wish
$(document).keypress(function(e) {
if(e.which == 13) {
console.log("User entered Enter key");
// the code you want to run
}
});
if you want to target a button once enter key is pressed you can use the code
$(document).bind('keypress', function(e){
if(e.which === 13) { // return
$('#butonname').trigger('click');
}
});
Hope it help