Reputation: 2174
is there a method in jquery that allows to catch when field is modified by script?
This only works when input field modified by hands
$('input[name=myInput]').change(function() { ... });
so as this
$('input[name=myInput]').on('input', function() { ... });
I want an event listener for this field to be able to catch this:
$('input[name=myInput]').val('changed_value');
Upvotes: 2
Views: 119
Reputation: 4700
I would also do as A. Wolff says, which is best way :
$('input[name=myInput]').val('changed_value').trigger('change');
Though, if you really want it to always trigger a change event when you programatically change it (but i don't recommend it !), you could override jQuery val function with something like this :
(function ($) {
var originalVal = $.fn.val;
$.fn.val = function(value) {
if (typeof value != 'undefined') {
// setter
originalVal.call(this, value);
this.trigger('change');
}else{
// getter
return originalVal.call(this, value);
}
};
})(jQuery);
Upvotes: 1