I'm looking for an example of how to simulate a keypress event in js.
The examples I've found all use the deprecated Event.initEvent method.
When I try using the recommended KeyboardEvent instead, it doesn't work - the input is not updated.
<input id="myInput" />
const element = document.getElementById('myInput')
const keypressEvent = new KeyboardEvent('keypress', {
code: 'KeyA'
})
element.dispatchEvent(keypressEvent)
What is wrong or missing in the above code? Does the element need to take focus first or something like that?