Let's say we have an input and we want to add some texts to it:
<form>
First name:<br>
<input type="text" id="thename" name="firstname">
</form>
The simple thing is to add value to input with classic element.value...
.
But what if I want to add for example 'David' by simulating the keypress
event?
I followed several approaches like this:
function simulateKeyEvent(character) {
var evt = document.createEvent("KeyboardEvent");
(evt.initKeyEvent || evt.initKeyboardEvent)("keypress", true, true, window,
0, 0, 0, 0,
0, character.charCodeAt(0))
var canceled = !body.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
}
Taken from here and read almost every solutions but they didn't work.
Note: I'm working in Firefox and Chrome