JavaScript Programmatically send keys to input using dispatchEvent

I'm trying to send characters to an input element based on user actions.

I'm trying to use KeyboardEvent with dispatchEvent but whatever I do, it doesn't work

For example:

let keyEvent = new KeyboardEvent();
keyEvent.key = 'a';
keyEvent.keyCode = 'a'.charCodeAt(0);
keyEvent.which = event['keyCode'];
keyEvent.altKey = false;
keyEvent.ctrlKey = true;
keyEvent.shiftKey = false;
keyEvent.metaKey = false;
keyEvent.bubbles = true;

Not sure if this is correct, but I have dispatched it as follows:

  document.querySelector('input').dispatchEvent(keyEvent);
  document.activeElement.dispatchEvent(keyEvent);
  document.dispatchEvent(keyEvent);

DEMO

If I first focus the input before clicking the button nothing really happens. Any suggestions what might go wrong here ?

Answer:1

Actually, due to security reasons, dispatching a KeyboardEvent to an input element does not generate the action you expect.

The KeyboardEvent document makes it quite clear:

Note: manually firing an event does not generate the default action associated with that event. For example, manually firing a key event does not cause that letter to appear in a focused text input. In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.

So the dispatchEvent stuff simply won't work.

Alternatively, consider manipulating the input element directly.

Answer:2

I want to find all word which start with mc and followed by all digits only var myString="hi mc1001 hello mc1002 mc1003 mc1004 mc mca" Require output = [ mc1001,mc1002,mc1003,mc1004] My solution: ...

I want to find all word which start with mc and followed by all digits only var myString="hi mc1001 hello mc1002 mc1003 mc1004 mc mca" Require output = [ mc1001,mc1002,mc1003,mc1004] My solution: ...

  1. find word start with
  2. how to find word startup folder
  3. word finder start with u
  4. find word start with st
  5. find word start with sc
  6. find word start with gl
  7. find word start with sh
  8. find word start with air
  9. find word start with cou
  10. find word start with bri
  11. find word start with qua
  12. find word starting with regex
  13. find word starting with python
  14. find word starting with x
  15. find word starting with hash
  16. find word starting with k
  17. find word starting with play
  18. word find start of line
  19. word finder start with c
  20. word finder start with v

I have created a custom css tooltip, however the tooltip width is very narrow (only 1-2 words per line). Fiddle example of the problem I would like the tooltip size to be dynamically generated, as ...

I have created a custom css tooltip, however the tooltip width is very narrow (only 1-2 words per line). Fiddle example of the problem I would like the tooltip size to be dynamically generated, as ...

How can I fix chart js bug like it shows old data when I move the mouse on the chart. My Js file $('#select-question').change(function(){ var questionId = $("option:selected",this).val(); $....

How can I fix chart js bug like it shows old data when I move the mouse on the chart. My Js file $('#select-question').change(function(){ var questionId = $("option:selected",this).val(); $....

  1. chart shows data changes
  2. chart shows data
  3. the chart shows data for four moving objects
  4. the chart shows data for four heat engines
  5. the chart shows data for a moving object
  6. the chart shows data for four different moving objects
  7. the chart shows data on the construction of dreadnoughts
  8. line chart shows data
  9. a run chart shows data in a static view
  10. chart js shows old data on mouseover
  11. which chart shows how data changes over time
  12. chart that shows 100 data points
  13. line chart show data points

I am integrating my client website with Google APIs, and I have a task to validate the user address by using the user's postcode (UK), and to get the address, I have used https://ideal-postcodes.co.uk/...

I am integrating my client website with Google APIs, and I have a task to validate the user address by using the user's postcode (UK), and to get the address, I have used https://ideal-postcodes.co.uk/...

  1. google maps google maps
  2. google maps google sheets
  3. google maps google car
  4. google maps google docs
  5. google maps google drive
  6. google maps google play store
  7. google maps google is not defined
  8. google maps google snake
  9. google maps google maps directions
  10. google maps google maps go
  11. google maps google my business
  12. google maps google play services error
  13. google maps google cloud
  14. google maps google street view
  15. google maps google flights
  16. google maps google earth satellite view
  17. google maps google translate
  18. google maps google headquarters
  19. google maps google sheets function
  20. google maps google maps satellite