Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind enter key to specific button on page

<input type="button" id="save_post" class="button" value="Post" style="cursor:pointer;"/> 

How can I bind the enter key on the persons keyboard to this specific button on the page? It's not in a form, and nor do I want it to be.

Thanks!

like image 495
slandau Avatar asked Jul 01 '11 00:07

slandau


People also ask

How do you trigger a button on a Enter key?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

How do you submit a form when Enter key is pressed?

To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.


1 Answers

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();     } }); 
like image 144
Chris Laplante Avatar answered Oct 19 '22 23:10

Chris Laplante