0

I am trying to create an alert when a date is selected that falls within 30 days of today using the following script:

(function ($) {
  $('#edit-select-date').datepicker({

    onSelect: function () {
      var date = $(this).datepicker('getDate');
      var today = new Date();

      if ((new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30)) > date) {
        // Do something here.
        alert(123);
      }
    }
  });
}(jQuery));

Unfortunately this is not working and am getting a $(...).datepicker is not a function in dev console. If datepicker.js is not being used is there a way I can have an alert trigger when the date select is +30 days from today?

1

Webform provides a fieldable form entity type. And when you add a date field to it (you didn't specify what date field exactly, core, contrib, custom), this date field may make use of a datepicker library.

Speaking of core it most likely is core's core/jquery.ui.datepicker library. Which is used for #type => 'date' form elements for example.

When you in your custom script want to make use of this datepicker library you need to add it as a dependency. Same as core does:

drupal.date:
  version: VERSION
  js:
    misc/date.js: {}
  dependencies:
    - core/drupal
    - core/modernizr
    - core/jquery.once
    - core/jquery.ui.datepicker

See https://git.drupalcode.org/project/drupal/blob/8.8.x/core/core.libraries.yml#L142-150.


And maybe you also make your JavaScript look more like in the examples in the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.