How to add jQuery Datepicker option which accepts a function as an argument I have added a date_popup field to a form :

$form['arrival_date'] = [
  '#type'                =&gt; 'date_popup',
  '#title'               =&gt; t('Arrival date'),
  '#date_format'         =&gt; 'd/m/Y',
  '#date_year_range'     =&gt; '0:+1',
  '#date_label_position' =&gt; 'none',
  '#default_value'       =&gt; '',
  '#size'                =&gt; '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.
You have to ommit the 'option' parameter, the following modified version of your code worked for me:

(function ($) {

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

})(jQuery);