jQuery execute a single action (method) for multiple elements

3

I have a form where an action can be executed multiple times by different elements, I would like to know if I can put everything in a single call. For example:

$("#campo_01").change(function(){
    $("#frmForm").submit();
});

$("#campo_02").change(function(){
    $("#frmForm").submit();
});

$("#campo_03").change(function(){
    $("#frmForm").submit();
});

Of course, it's not that simple, because the functions are more complex, with a few more lines. The above example is just to understand what I'm looking for.

Is it possible to put everything together, for example:

$("#campo_01 #campo_02 #campo_03").change(function(){
    $("#frmForm").submit();
});
    
asked by anonymous 24.11.2015 / 22:20

1 answer

3

You can detach by commas within the selector string like this:

$("#campo_01, #campo_02, #campo_03").change(function(){
    $("#frmForm").submit();
})

or even with the ^= selector, which means id "starting with" :

$('[id^="campo_"]').change(function(){
    $("#frmForm").submit();
})
    
24.11.2015 / 22:27