Skip to content Skip to sidebar Skip to footer

Fire "onchange" Event On Page From Google Chrome Extension

I am developing an extension to autofill a form with data from another website. I have a context menu with different options, each with different data but complete the same form. T

Solution 1:

Have you tried emitting the event rather than directly calling the function? As far as I remember, calling event-handlers directly can have side effects. I can't find a source at the moment though.

Try to replace

document.getElementsByName("country")[0].onchange();

with

var changeEvent = document.createEvent("HTMLEvents");
changeEvent.initEvent("change", true, true);
document.getElementsByName("country")[0].dispatchEvent(changeEvent);

I also created a little fiddle: http://jsfiddle.net/d94Xb/1/

Furthermore you might look into addEventListener() and dispatchEvent() a bit more as this is the common way to register and execute event handler.


(update: 2015-04-27) The method described above is deprecated by now as @10basetom points out. The better way is to use Event(). Please note that this won't work in IE and Safari.


Post a Comment for "Fire "onchange" Event On Page From Google Chrome Extension"