Is there any way to simulate an enter key press in JavaScript when the user does some stuff?
Asked
Active
Viewed 1.3k times
-8
-
Yes.https://www.google.com/search?q=js+trigger+keypress&oq=js+trigger+keypress – Jonas Wilms Jul 12 '17 at 09:50
-
Using jquery, then doing a dispatch – Álvaro Touzón Jul 12 '17 at 09:50
-
@ÁlvaroTouzón Is jQuery capable to search too? – Teemu Jul 12 '17 at 09:51
-
https://www.w3schools.com/jsref/event_onkeypress.asp – RaV Jul 12 '17 at 09:52
-
https://msdn.microsoft.com/en-us/library/ms536939(v=vs.85).aspx – Gowtham Shiva Jul 12 '17 at 09:54
1 Answers
0
You can use:
var e = jQuery.Event("keypress");
e.which = 13; //enter keycode
e.keyCode = 13;
$("#theInputToTest").trigger(e);
The trigger
function simulate an event (for example a click, a key pressed...).
More information here
Description: Execute all handlers and behaviors attached to the matched elements for the given event type.
There is a question like yours with answers here.

Albert Lazaro de Lara
- 2,540
- 6
- 26
- 41
-
-
See this answer for pure JS: https://stackoverflow.com/a/18937620/188837 Angular which version? – Albert Lazaro de Lara Jul 12 '17 at 10:21