3

I'm trying to replace a text in a textarea using TextEvent/textInput events like this:

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: The portion of text is well selected, but it isn't changed.

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

What am I missing?

Note: I'm using TextEvent instead of just replacing the text via val() because I want to use the Undo/Redo system.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cyril N.
  • 38,875
  • 36
  • 142
  • 243

0 Answers0