jQuery Datepicker : How to add option which accepts a function as an argument

01/07/2018


I have added a date_popup field to a form :

                $form['arrival_date'] = array(
                  '#type' => 'date_popup',
                  '#title' => t('Arrival date'),
                  '#date_format' => 'd/m/Y',
                  '#date_year_range' => '0:+1',
                  '#date_label_position' => 'none',
                  '#default_value' => '',
                  '#size' => '10',
                );

I want to add an option to run a function using the onClose option so my js is :

(function ($){

  Drupal.behaviors.ios2 = {
    attach: function (context, settings) {
    $('#edit-arrival-date-datepicker-popup-1').datepicker('option',{
            onClose: function (date) {
              //date will have the new date or empty string if no date has been selected
              alert(date);
              // Do something

            }
    });
  }
};

})(jQuery);

However the onClose event never executes. If I remove the ‘option’ argument thus :

$('#edit-arrival-date-datepicker-popup-1').datepicker({

it does work, but my suspicion is that it is replacing the datepicker with a new one, rather than altering the existing options.

قالب وردپرس