Javascript keyboard forced event inside text area in IE

I am trying to force a key inside a text box using Javascript. This should work specifically in IE, but just doesn't work.

Can anybody help me?

Here is my test script:

<html> <body> <input type="text" id="txtfld"> <input type="button" onclick="go()"> <script> function go() { var q = document.getElementById('txtfld'); q.style.backgroundColor='yellow'; q.focus(); var evObj = document.createEventObject(); evObj.keyCode = 84; // [T] key q.fireEvent('onkeypress', evObj); } </script> </body> </html> 
+4
source share
1 answer

It is not recommended that you try to manage the default browser events-events by falsifying events. To the extent that this can be done at all, it is browser-specific and unreliable.

If you want to add the letter 't to the field, say this:

 q.value+= 't'; 

In more complex cases, for example, if you want to insert a letter at the current cursor position, you need a branch code for document.selection (IE) and field.selectionStart/End (others).

+2
source

All Articles