Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Automate string input to a input

I know this question has been asked a hundred times. But once again, the solutions no longer work.

I need to simulate key presses to automate a form.

If I do it like this

document.getElementById("input_id").value = "testinput"

the events from the input do not trigger. And my value is just deleted again.

is there again a possibility to simulate keystrokes so that also all events are triggered?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can dispatch keyboard events on an EventTarget (element, Window, Document, others) like this:

element.dispatchEvent(new KeyboardEvent('keydown', {'key': 'a'}));

Example:

let element = document.querySelector('input');
element.onkeydown = e => alert(e.key);
changeValButton.onclick = () => element.value += "a";
dispatchButton.onclick = () => {
  element.dispatchEvent(new KeyboardEvent('keydown',{'key':'E'}));
  element.dispatchEvent(new KeyboardEvent('keyup',{'key':'E'}));
}
<input type="text" value="">
<button id="dispatchButton">Press to dispatch event </button>
<button id="changeValButton">Press to change value </button>

about 4 years ago · Juan Pablo Isaza Report

0

I have now found the following code that works for me:

function set_value(doc, input_value) {
  doc.value === undefined ? doc.innerHTML = input_value : doc.value = input_value;
  events = ["keydown", "keypress", "input", "keyup", "change"]
  for (var i = 0; i < events.length; ++i) doc.dispatchEvent(new Event(events[i], {
    bubbles: true
  }));
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!