Soldarnal Soldarnal -4 years ago 116
Javascript Question

How do I programmatically force an onchange event on an input?

How do I programmatically force an onchange event on an input?

I've tried something like this:

var code = ele.getAttribute('onchange');
eval(code);


But my end goal is to fire any listener functions, and that doesn't seem to work. Neither does just updating the 'value' attribute.

Answer Source

ugh don't use eval for anything. Well, there are certain things, but they're extremely rare. Rather, you would do this:

document.getElementById("test").onchange()

Look here for more options: http://jehiah.cz/archive/firing-javascript-events-properly

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download