Ergec Ergec - 4 months ago 21
jQuery Question

.val() doesn't trigger .change() in jquery

I am trying to trigger

.change()
on a text box when I change it's value simply with a button but it doesn't work. Check this link. If you type something in textbox then click somewhere else
.change()
triggers but if you click only button, it changes textbox value but
.change()
doesn't trigger.

Why?

Answer

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");
Comments