Detect when input value is changed via js

2

I have third system that fills a field by zooming. Below this zoom I have a grid of products (my specific). I need to include a product on this grid whenever the system fills this product field. However, there is no customization point that can intercept the moment the product is zoomed in.

The question is: How can I detect that the field value has changed (remembering that this is changed via javascript)? I've already tried using the events: bind and on of jquery to no avail. Another gambiarrenta alternative was to use settimeout . For now I'm looking for a more elegant way to solve this situation.

Has anyone ever had anything like this?

    
asked by anonymous 09.09.2015 / 20:58

3 answers

1

This may be possible in the future. A new method that can solve this problem is foreseen, the Object.observe ( ) , promised for ES7.

If you go forward you can pass an object to the method and a callback that will be called every time there is a change to object properties. In your case it should be called with the event update .

But this still does not exist and one of the polyfillque already exists does not work in this case ( link ).

So two possibilities remain. One of them has occurred to you too, which is to use setInterval or setTimeout .

The other one is half brute force, and works if the external code uses jQuery. The idea is to change the .val() method of jQuery to trigger events when changing values. I do not guarantee that it will not screw up but in this case it works ( link ).

var jVal = $.fn.val;
$.fn.val = function(v) {
    if (typeof v != 'undefined') jVal.call(this, v);
    else return jVal.call(this);
    if (v) $(this).trigger('change');
}
    
09.09.2015 / 22:18
0

If you can modify the function that changes the value of the field, I believe that the problem is easily solved by adding the line $("#teste").trigger('change'); just below the line that modifies the value of the field. So whenever the value is modified the event change will be triggered and the handler of the event will capture and treat it.

    
09.09.2015 / 22:25
0

Thank you very much for your support. The option I am using was setInterval. Even though it was not the most elegant option, the code did not look that bad. I used the following link: james.padolsey.com/javascript/monitoring-dom-properties

    
10.09.2015 / 15:36