A few days ago, I posted a question on how to update text in Internet Explorer. As it turned out, the method used also does not work in Firefox.
This made me wonder if there is a way to change the value of the text field and update the undo / redo queue (calling ctrl-Z or document.execCommand('undo'); )
So far I have found two possibilities, but they do not work in all browsers:
Option 1:
var event = document.createEvent('TextEvent'); event.initTextEvent('textInput', true, true, null, text, 9, "en-US"); textarea.focus(); textarea[0].setSelectionRange(selection.start, selection.end); textarea[0].dispatchEvent(event);
Note. It doesn't seem to work in IE (in general) and Firefox
Option 2:
document.execCommand("insertText", false, "the text to insert");
It does not work in IE (tested under 9, but does not seem to be implemented at all), I do not know other browsers.
source share