I need to change the value of input, but when I change it like element.value = 'spam'
, onChange event doesn't happen. In the inspector I see that there is onChange event for input element, but when I do element.onchange()
, I get TypeError: "element.onchange is not a function"
. How can I do it?
Asked
Active
Viewed 111 times
0

tiesto228
- 25
- 3
1 Answers
0
[Stackoverflow-Post] How can I trigger an onchange event manually?
Like said in this post. You can simply fire/trigger this onChange event.
element.onchange();
or
if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
element.dispatchEvent(evt);
}
else
element.fireEvent("onchange");
But if in your case element.onchange()
does not work, you can try the other examples above.
But can you please add your source code to your question... So we can help you further.

Jonathan
- 124
- 9
-
My script works with a web app and functions for all events of its elements are in the external script. When I needed to trigger onclick event I used element.click() and it worked but here I can change the value of input but can't make the app see it was changed. – tiesto228 Apr 09 '20 at 18:54