fxswiss
fxswiss

Reputation: 23

Trigger javascript function when any of multiple fields change (onchange)

I have a basic JS knowledge.

I need to trigger the function below when any of the fields change in a from. Here it works only when the var2 changes but I need it to be triggered when any of the var# will change. (I will have 30 var at the end in the form)

I think I need to have an OR condition, "if (var1.onchange OR var2.onchange OR ...) function()"

Thanks in advance for your valuable input!

<script>
window.onload = function () {
var var0 = document.getElementById('field09')
    var1 = document.getElementById('field10')
    var2 = document.getElementById('field11')
    storage = document.getElementById('field12')

var2.onchange= function()


  { 
storage.value = var0.value+" ; "+var1.value+" ; "+var2.value;
};
};
</script>

Upvotes: 1

Views: 2051

Answers (1)

frogatto
frogatto

Reputation: 29285

Please try this:

window.onload = function () {
    var var0 = document.getElementById('field09'),
        var1 = document.getElementById('field10'),
        var2 = document.getElementById('field11'),
        storage = document.getElementById('field12'),

        trigFunc = function(){ 
            storage.value = var0.value + " ; " + var1.value + " ; " + var2.value;
        }

    var0.onchange = trigFunc;
    var1.onchange = trigFunc;
    var2.onchange = trigFunc;
}

Demo: jsfiddle

Upvotes: 2

Related Questions