Replace text in textarea with the TextInput event

I am trying to replace text in a text field using TextEvent / textInput events as follows:

var textarea = $('#my_textarea'), text = "Hello world !", selection = {'start': 4, 'end': 6}; 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); 

This works fine in Chrome, but not in Internet Explorer 9: part of the text is well selected, but it does not change: /

I followed what was written here: Javascript textarea undo redo and here http://help.dottoro.com/ljuecqgv.php with no luck.

What am I missing?

Thank you for your help!

Note. I use TextEvent instead of just replacing text with val() , because I want to use the Undo / Redo system.

+2
source share

Source: https://habr.com/ru/post/957511/


All Articles