简体   繁体   中英

Fire onchange event when hidden input changes

I have various inputs that I want to set a dirty flag.

However the onchange event does not fire if these inputs are modified by javascript.
Many of the inputs are customised data entry moodules written by many other people and have hidden input and javascript UI layer to modify the hidden input.

I want to detect when these hidden inputs change. (without modifying the original author's script)
Also normal inputs do not trigger the 'onchange' event until 'blur' but I could get round this with the onkeyup event.

I know this is an old question, but I had the same one, and I found the answer here: What event can be captured when an HTML hidden input value is set / changed

EDIT: "Check out ' domattrmodified ' event.

Just a thought, because you dont want to modify the original authors script.

Could you maybe monitor those fields?

/*
Very simple monitor utility.

Monitor what?
This monitors any dom element which has a "value" attribute.

This was created to monitor hidden fields that were altered via javascript.

Fields that had values changed via javascript did not trigger the "change" event.
This is a solution to that problem.


Usage:

Monitor.monitor('element_id',(function) callback);

Monitor.demonitor('element_id');


*/


var Monitor = (function(){
    var inputs = [];
    return {
        monitor:function(input_id,callback){
            var previous_value = "";
            function go (){
                current_value = document.getElementById(input_id).value;
                if(previous_value !== current_value){
                    callback();
                }
                previous_value = document.getElementById(input_id).value;
            }
            inputs[input_id] = setInterval(go,200);
        },

        demonitor:function(input_id){
            clearInterval(inputs[input_id]);
        }


    }
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM