function myFunc() { alert("left pressed"); } $("#next").click(myFunc); $(document).keypress(function(e) { if(e.keyCode == 37) myFunc(); });
You can also have myFunc () handle both events, for example ..
function myFunc(e) { // First we check to see if this event has a keyCode property // If so, then we need to check the value of that keyCode. // If it doesn't match the value we're trying to capture, // then we just "exit" the function by returning false. if(e.keyCode && e.keyCode != 37) { return false; } alert("left pressed"); } // Since myFunc() handles both scenarios, we can // bind it to both objects. $("#next").click(myFunc); $(document).keypress(myFunc);
source share