Main menu

Drupal: jQuery UI Datepicker "setDate" issue

Published by dnovikov on Tue, 09/13/2011 - 17:55

A lot of posts across the Web tell about issues with applying "setDate" function in jQuery UI Datepicker. In Drupal environment it is especially bad because you can't modify (or don't want to modify) source code used for datepicker instances initialisation. Here is a helper function which can save you from madness in some cases:

function datepickerFieldInit(el) {
  // If we don't apply .datepicker with 'setDate' option
  // we won't be able to change date with further calls to 'setDate' method
  // 'dateFormat' is also neccessary here for unknown reasons
  $(el).datepicker({
    setDate: 'c',
    dateFormat: 'dd/mm/yy'
  });
  $(el).datepicker('setDate', $(el).val());
}

You can use this function as following:

datepickerFieldInit('your-input-field');
$('your-link').click( function() {
   $('your-input-field').datepicker('setDate', 'c-1m');
   return false;
});

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.