Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery simulating keypress event on an input field

Tags:

I have an input field which is dynamically filled with some AJAX, and once it's filled I would like to have JavaScript to automatically press "Enter" (key event 13) on that input inducing another function, which is hidden to me, to trigger.

Would it be possible with jQuery? I have this code right now, which is not working:

$('#myInputId').focus().trigger({ type : 'keypress', which : 13 });

Do you have any clue on what am I doing wrong?

like image 889
Masiar Avatar asked May 25 '11 12:05

Masiar


People also ask

Can JavaScript simulate key presses?

So with the JavaScript KeyboardEvent API (developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent) you can simulate key events, for example say that a key is pressed but it is not possible to have the key press effect, like writing characters 'a', 'b', 'c'?

How does jQuery detect keyboard press?

The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.

What is Keyup and Keydown in jQuery?

keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.

How do you press a key in JavaScript?

To record a keypress event in JavaScript, use the code below: // Add event listener on keypress document. addEventListener('keypress', (event) => { var name = event. key; var code = event.


1 Answers

You can create an event object:

$('#myInputId').trigger(jQuery.Event('keypress', { keycode: 13 })); 
like image 102
Felix Kling Avatar answered Sep 30 '22 17:09

Felix Kling