Clive_Bigsby
Clive_Bigsby

Reputation: 189

Checking to see if 2 jquery ui datepickers have value to perform function

I'm trying to check and see if 2 jquery ui datepickers have a value, if they do, perform a function, if not, to do nothing.

$("#datepicker").datepicker({
        onSelect: function () {

        }
    });
    $("#datepicker2").datepicker({
        onSelect: function () {

        }
    });

I think I need to use the callbacks to see if a date has been selected, but I'm stuck on how to check those callbacks to see if they have happened.

Upvotes: 1

Views: 42

Answers (1)

RafH
RafH

Reputation: 4544

Here is a way using the getDate method of jquery ui datepicker. It returns null if there is no date selected.

$("#datepicker,#datepicker2").datepicker({
   onSelect: checkDates
});

function checkDates () {
    var ok = $('.hasDatepicker')
                       .filter( function() { 
                                   return $(this).datepicker('getDate') !== null
                              })
                       .length == 2;
      if (ok === true) {
        // doSomething 
      }     
}

datepicker have the "hasdatepicker" css class added on construct so $('.hasDatepicker') return all datepickers, then filter the result to get an array of item with a date !== null, then compare length to know if they are all filled.

You can replace 2 by $('.hasDatepicker').length if you want to check if all datepicker have a date selected.

Fiddle

Upvotes: 1

Related Questions