Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one call on change of one of two inputs

Tags:

jquery

I have the code below in a page, if one of two textfields changes a function is getting called with the values of both.
The code works perfectly fine, but in the interest of efficiency I was wondering if I can boil it down to a single block of code.

$('#popfrom').change(function(){
    var popfrom = $('#popfrom').val();
    var poptill = $('#poptill').val();

    defineDate(popfrom, poptill);
});

$('#poptill').change(function(){
    var popfrom = $('#popfrom').val();
    var poptill = $('#poptill').val();

    defineDate(popfrom, poptill);
});
like image 292
nhalink Avatar asked Mar 11 '13 15:03

nhalink


2 Answers

JQuery allows multiple selectors, comma-separated:

$('#popfrom, #poptill').change(function(){ ...

If you find you need to work with multiple elements, it's often easier to use a class name instead. Add a class of "popfields" to your inputs and you can simply use:

$('.popfields').change(function(){ ...
like image 184
Diodeus - James MacFarlane Avatar answered Sep 28 '22 09:09

Diodeus - James MacFarlane


Add them to the same class, then execute.

$('.popClass').change(function(){
    var popfrom = $('#popfrom').val();
    var poptill = $('#poptill').val();

    defineDate(popfrom, poptill);
});
like image 32
tymeJV Avatar answered Sep 28 '22 07:09

tymeJV