Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger the enter keypress

I need to know how to trigger an enter key on an input. That is, in response to some other event (not a keypress), I need to trigger a keypress of value 13.

(Clarification, I do not want to trigger some event when the enter key is pressed. If I didn't know how to do that, I could find that that's been asked and answered several times on SO. I want the reverse. I am trying to develop a workaround that requires I emulate an 'enter' keypress.)

like image 848
Derek Henderson Avatar asked Jun 20 '13 15:06

Derek Henderson


People also ask

How do I submit a Enter key?

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.

How can I execute a function on pressing the Enter key in an input field?

You can execute a function by pressing the enter key in a field using the key event in JavaScript. If the user presses the button use the keydown to get know its enter button or not. If it enters the key then call the JavaScript function.

How do you detect enter press?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.


1 Answers

You can do this -

var e = $.Event( "keypress", { which: 13 } ); $('#yourInput').trigger(e); 
  • http://api.jquery.com/category/events/event-object/
  • http://api.jquery.com/trigger/
like image 126
Mohammad Adil Avatar answered Sep 24 '22 10:09

Mohammad Adil