6

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.

plumsmugler
  • 111
  • 1
  • 6
  • why not redirect the user to the page instead? yoururl.com/search?page=1 – eestein Mar 25 '16 at 19:52
  • Because this isn't part of a form and it's not going to another page. It essentially manipulates a dynamic element in the middle of the page. – plumsmugler Mar 25 '16 at 19:55
  • never mind, my bad, I should've paid more attention to your question, it's about a specific plugin. – eestein Mar 25 '16 at 19:56
  • do they provide an api? or source code so you can trigger the next page or specific page click? – eestein Mar 25 '16 at 19:58
  • I do have access to the source code but it is impenetrable. Thousands of lines of single spaced text with each line containing thousands of characters. I'm trying to hunt down documentation right now. – plumsmugler Mar 25 '16 at 20:05

1 Answers1

1

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

RobinF
  • 349
  • 1
  • 2
  • 17
  • Close but no cigar. I am able to input a value into the input field using your first line of code, but from what I can tell this is not within a form field and there is no submit command. – plumsmugler Mar 25 '16 at 20:11
  • @plumsmugler, we do not know what the page is like, so please share a sample of the website's HTML/source that you are trying to interact with. – Spencer D Mar 25 '16 at 20:12
  • 1
    I don't think source is necessary. I'm asking a hypothetical question really but the most basic source code for such a scenario would be If I hit that button I want a number to appear within that input field and then simulate a return key stroke. – plumsmugler Mar 25 '16 at 20:15
  • @plumsmugler: http://stackoverflow.com/questions/3276794/jquery-or-pure-js-simulate-enter-key-pressed-for-testing – Spencer D Mar 25 '16 at 20:24