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?
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'?
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.
keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.
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.
You can create an event object:
$('#myInputId').trigger(jQuery.Event('keypress', { keycode: 13 }));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With