Detect Programmatic Changes on Input Type Text

Detect programmatic changes on input type text

If you're dealing with a modern browser, you can try with something like this:

var input = document.getElementById('test');
input._value = input.value;
Object.defineProperty(input, "value", {
get: function() {return this._value;},
set: function(v) {
// Do your stuff
this._value = v;
}
});

This solution is good if you actually don't expect any user input (i.e., hidden type input fields), because it's extremely destructive of the DOM basic functionality. Keep that in mind.

How to detect (programmatic) changes to textarea?

I eventually found an answer to this here: Textarea onchange detection

Using

<textarea oninput="myfunc(this)"  onpropertychange="myfunc(this)">

solved it for me, and this is supposed to work for IE8 as well.

jQuery detect programatic change to field

You should be able to trigger the change event with:

$('#myHiddenField').change();

OR

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

Of course this will require the block of code responsible for updating the filed to make one of those calls after it has done its work.

How do I detect an inputfield value change event if changed via jquery?

Simply use: $("input#DonationAmount").trigger('change'); and that should do it.

Detect all changes to a input type=text (immediately) using JQuery

Unfortunately, I think setInterval wins the prize:

<input type=text id=input_id />
<script>
setInterval(function() { ObserveInputValue($('#input_id').val()); }, 100);
</script>

It's the cleanest solution, at only 1 line of code. It's also the most robust, since you don't have to worry about all the different events/ways an input can get a value.

The downsides of using 'setInterval' don't seem to apply in this case:

  • The 100ms latency? For many applications, 100ms is fast enough.
  • Added load on the browser? In general, adding lots of heavy-weight setIntervals on your page is bad. But in this particular case, the added page load is undetectable.
  • It doesn't scale to many inputs? Most pages don't have more than a handful of inputs, which you can sniff all in the same setInterval.


Related Topics



Leave a reply



Submit