val() doesn't trigger change() in jQuery

Ergec picture Ergec · Jul 5, 2010 · Viewed 254.6k times · Source

I'm trying to trigger the change event on a text box when I change its value with a button, but it doesn't work. Check this fiddle.

If you type something in the text boxes and click somewhere else, change is triggered. However, if you click the button, the text box value is changed, but change doesn't trigger. Why?

Answer

djdd87 picture djdd87 · Jul 5, 2010

onchange only fires when the user types into the input and then the input loses focus.

You can manually call the onchange event using after setting the value:

$("#mytext").change(); // someObject.onchange(); in standard JS

Alternatively, you can trigger the event using:

$("#mytext").trigger("change");