Aditya K
Aditya K

Reputation: 159

Is there any way to manually invoke "Enter key" event on a textbox?

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

Answers (3)

techfoobar
techfoobar

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

Ruan Mendes
Ruan Mendes

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

Didier Ghys
Didier Ghys

Reputation: 30666

Trigger the event with jQuery:

$(document).trigger('keypress');

Upvotes: 0

Related Questions