Reputation: 159
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.
Upvotes: 6
Views: 3010
Reputation: 66663
You 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
Upvotes: 8
Reputation: 92274
You are looking for synthetic events. It's quite complicated. The following library takes care of it for you... http://jupiterjs.com/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" )
});
Upvotes: 0
Reputation: 30666
Trigger the event with jQuery:
$(document).trigger('keypress');
Upvotes: 0