7

I'm working with jquery.

And i have text input in a form, i would like to process the change event.

$("#my_input").change(function(){alert('123');});

the event is fired normally when i edit the field manually, but when i change the field's value with a script (sometimes with an external script) it doesn't work.

is there any solution ?

and thanks in advance.

CAbbott
  • 8,078
  • 4
  • 31
  • 38
Youcef04
  • 187
  • 1
  • 3
  • 7
  • Does this answer your question? [val() doesn't trigger change() in jQuery](https://stackoverflow.com/questions/3179385/val-doesnt-trigger-change-in-jquery) – Eugen Konkov Oct 06 '21 at 08:46

4 Answers4

12

The change event is not designed to detect programmatic changes. You can trigger the event manually when setting the value, though:

$('#my_input').trigger('change');

shortcut:

$('#my_input').change();
jwueller
  • 30,582
  • 4
  • 66
  • 70
  • 1
    is there an event that fires when the input is changed programmatically ? – Youcef04 Mar 21 '12 at 13:10
  • @Youcef04: No, JavaScript does not support that kind of monitoring. Some other languages have constructs that allow stuff like this, but not in JavaScript. – jwueller Mar 21 '12 at 13:12
  • @Youcef04: It would be nice if you could accept this answer if I was able to help you :) – jwueller Mar 21 '12 at 15:29
0

Fire them manually, using one of the following two functions:

$('#my_input').trigger('change');
$('#my_input').change();
callumacrae
  • 8,185
  • 8
  • 32
  • 49
0

You can use live/delegate to bind your change event

$("#my_input").live('change',function(){alert('123');}); 
Misam
  • 4,320
  • 2
  • 25
  • 43
0

here is the code

$("input[type='text']").change( function() {
  // check input ($(this).val()) for validity here
});