0

I want to simulate keypress in javascript without jQuery I always test my code in browser, developer console

And I tried this element.dispatchEvent(new KeyboardEvent('keydown',{'key':'e', 'keyCode': 69}));

but I does nothing the input element doesn't change nor increased length

How can I simulate key press, I want something new I know element.value = "Hello World" but I need to learn the dispatchEvent to simulate return key

  • 1
    `dispatchEvent` events are usually handled by `addEventListener` handlers - input elements do not "listen" for such events - my explanation is flawed, but I know what I mean :p – Jaromanda X Jun 16 '21 at 06:57
  • I can see no use case where I must simulate a return key. What is the higher goal of your software? – Thallius Jun 16 '21 at 07:14
  • Does this answer your question? [How do I simulate a mouseover in pure JavaScript that activates the CSS ":hover"?](https://stackoverflow.com/questions/17226676/how-do-i-simulate-a-mouseover-in-pure-javascript-that-activates-the-css-hover) – Coloured Panda Jun 16 '21 at 07:28
  • So far it's an exact duplicate of https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically - if this line doesn't work, you should show some context, a [mre] – tevemadar Jun 16 '21 at 07:36

0 Answers0