I want to manually invoke enter key press event on textbox through JS or jQuery.
I don't want to capture that event. I just want to invoke enter key press event.
I want to manually invoke enter key press event on textbox through JS or jQuery.
I don't want to capture that event. I just want to invoke enter key press event.
Share Improve this question edited Nov 28, 2022 at 19:11 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 22, 2011 at 18:52 Aditya KAditya K 1591 gold badge4 silver badges13 bronze badges 2- 3 Do you want to submit a form? – David Hedlund Commented Dec 22, 2011 at 18:54
- What do you want to achieve? Should that event trigger the existing key handler bound to the textbox? – Šime Vidas Commented Dec 22, 2011 at 18:56
3 Answers
Reset to default 8You can trigger it like:
// dummy event listener
$("input").keydown(function() { return true; });
var keyEvent = jQuery.Event("keydown");
keyEvent.keyCode = 13;
$("input").trigger(keyEvent);
See this post too: Definitive way to trigger keypress events with jQuery
Trigger the event with jQuery:
$(document).trigger('keypress');
You are looking for synthetic events. It's quite plicated. The following library takes care of it for you... http://jupiterjs./news/syn-a-standalone-synthetic-event-library
Here's example code using their lib
Syn.click( {},'hello' )
.type( 'Hello World' )
.delay() //waits 600ms seconds by default
.drag( $('#trash') , function() {
ok( $('#hello').length == 0, "removed hello" )
});