How to fire JQuery change event when input value changed programmatically?

Mohammad picture Mohammad · Nov 24, 2015 · Viewed 52.3k times · Source

I want to fire the JQuery change event when the input text is changed programmatically, for example like this:

But it doesn't work. How can I make this work?

Answer

Pranav C Balan picture Pranav C Balan · Nov 24, 2015

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

You need to trigger the event manually using change() or trigger('change')

$("input").change(function() {
  console.log("Input text changed!");
});
$("input").val("A").change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='text' />