Search code examples
javascriptjquery

OnInput event in Chrome not working any more


The code below used to work in Chrome till version 52.x but in the newest version it does not work any more. Why might this be?

https://jsfiddle.net/2jtrs2m2/

<input type="text" id="test" />

<script type="text/javascript">

var my_event = document.createEvent("TextEvent");    
my_event.initTextEvent("textInput",true,false,window,"T");

document.getElementById("test").focus();

document.getElementById("test").dispatchEvent(my_event);

</script>

Usually when I executed this code, Chrome would insert the character T in the input. I don't want to use document.getElementById("xxx").value = "T" because in some very specific cases I am working on, I really can only use the code above firing the oninput event which for some reason is not working any more.


Solution

  • Try using document.execCommand ie; document.execCommand("insertText", false, "foo");