Is there a way to manually trigger the "Enter key" event in a text box?

I want to manually trigger an input keypress event in a text field via JS or jQuery.

I do not want to capture this event. I just want to trigger an enter keypress event.

+6
source share
3 answers

You can call it like this:

// dummy event listener $("input").keydown(function() { return true; }); var keyEvent = jQuery.Event("keydown"); keyEvent.keyCode = 13; $("input").trigger(keyEvent); 

See also this post: Determining how to trigger keypress events using jQuery

+8
source

Run the event using jQuery:

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

You are looking for synthetic events. This is pretty tricky. The following library will take care of this for you ... http://jupiterjs.com/news/syn-a-standalone-synthetic-event-library

Here is sample 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" ) }); 
0
source

Source: https://habr.com/ru/post/904338/


All Articles