Trigger inline onchange javascript
-
28-09-2019 - |
Question
I'm writing a greasemonkey script that creates an auto-complete search type box which makes it easier to select from a large dropdown on a webpage. The dropdown has inline onchange code which I can't seem to get to trigger when I change the selection using javascript. Any ideas?
Solution
Suppose the the page had something like:
<input onchange="someFunction()">
Then your Greasemonkey JavaScript could change the input value and then call the function using:
unsafeWindow.someFunction();
OTHER TIPS
You can trigger the onchange "really":
From within GM:
unsafeWindow.dropdownObject.onchange();
From the webpage:
dropdownObject.onchange();
For tampermonkey, this works simply without using unsafewindow.
Suppose the dropdownbox is defined in webpage something like:
<select id="dd_id", onchange="myfunction()">
Then following code in userscript triggers the onchange event:
document.getElementById('dd_id').onchange();
Also, it is suggested to avoid use of unsafewindow in documentation.