开发者

Fire onchange event when hidden input changes

开发者 https://www.devze.com 2023-03-26 23:27 出处:网络
I have various inputs that I want to set a dirty flag. Howeve开发者_C百科r the onchange event does not fire if these inputs are modified by javascript.

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

Howeve开发者_C百科r 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]);
        }


    }
})();
0

精彩评论

暂无评论...
验证码 换一张
取 消