This is my code:
<script>
function f(){
var i=document.getElementById("i");
i.focus();
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent("keypress", true, true, null, false, false, false, false,
0, 32);
i.dispatchEvent(evt);
}
</script>
<body onload="f();">
<input id="i"/>
</body>
Open the script in firefox and it works. The empty space inside the input field indicates that the code worked.
However, the above code snippet does not work in Chrome, Safari, Opera, etc.
How can we modify the above code to work in these browsers?
source
share