Jquery change () doesn't work if value is changed using jQuery

I have an input text field that I did readonly so the user could not enter it. However, I am updating its value with jquery. So basically I want the event to fire when its value is changed using jQuery. The following is an example of input markup and jquery markup.

<input type="text" readonly="readonly" id="textinput" /> $('#textinput').change(function() { alert("It Changed!"); }); 
+4
source share
3 answers

You cannot do it. Changes made using code do not raise events. You can simply invoke change directly in your code.

Something like that:

 // do stuff $('#textinput').val("something").change(); 
+17
source

I think the "change" event is fired only when the text field loses focus. Therefore, after changing the value in the code, you will need to do -

 $('#textinput').trigger('change') 

Demo - http://jsfiddle.net/upS2x/1/

+3
source

Since it is a read-only input, you cannot enter it, so changing, keyup, keydown and keystroke will not work with respect to this input.

In doing so, you must apply the event to a function that changes the input text. Therefore, if input1 changes the text in textinput:

 $("#input1").keyup(function(){ $("#textinput").val($(this).val()); }); 

I hope this helps.

+2
source

All Articles