Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate key strokes into a text input field

Using JavaScript/JQuery I'd like to have a line of code that will simulate a series of key strokes. Specifically I'd like line of code that:

  1. Simulates a click onto a text input field on another part of the page
  2. Enter a number into that text input field (I've already figured this part out)
  3. Simulate hitting the return key

The idea being I'm using a flipbook plugin and there's a page search field as part of the plugin. I'd like to create a button that quickly takes you to a specific page. The easiest way I've figured to do that is simulate a series of key strokes after the button is clicked which acts as if the user clicked into the page search field and entered in a page number then hit return.

like image 627
plumsmugler Avatar asked Mar 25 '16 19:03

plumsmugler


1 Answers

Could u maybe use:

document.getElementById('myTextarea').value = '';

document.getElementById("myForm").submit();

Where the first line replaces the text in the text area and the second one submits it

If you really need to use the return key, and can use jQuery:

var e = $.Event("keydown", { keyCode: 13});  //I think it is 13
$("body").trigger(e)

Or

var e = jQuery.Event("keypress");
e.which = 13; //choose the one you want
e.keyCode = 13;
$("#theInputToTest").trigger(e)

EDIT: Last one is also mentioned in the comments

like image 187
RobinF Avatar answered Sep 26 '22 15:09

RobinF