Bind enter key to specific button on page Bind enter key to specific button on page jquery jquery

Bind enter key to specific button on page


This will click the button regardless of where the "Enter" happens on the page:

$(document).keypress(function(e){    if (e.which == 13){        $("#save_post").click();    }});


If you want to use pure javascript :

document.onkeydown = function (e) {  e = e || window.event;  switch (e.which || e.keyCode) {        case 13 : //Your Code Here (13 is ascii code for 'ENTER')            break;  }}


using jQuery :

$('body').on('keypress', 'input', function(args) {    if (args.keyCode == 13) {        $("#save_post").click();        return false;    }});

Or to bind specific inputs to different buttons you can use selectors

$('body').on('keypress', '#MyInputId', function(args) {    if (args.keyCode == 13) {        $('#MyButtonId').click();        return false;    }});