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);
});
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(){ ...
Add them to the same class, then execute.
$('.popClass').change(function(){
var popfrom = $('#popfrom').val();
var poptill = $('#poptill').val();
defineDate(popfrom, poptill);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With