1

if ($(".section").length) {
  var script_arr = [
    'moment.js',
    'daterangepicker.js',
  ];
  $.getMultiScripts(script_arr);
}
$(function() {
  $('#date-picker').daterangepicker({
    "opens": "left",
    singleDatePicker: true,
    isInvalidDate: function(date) {
      var disabled_start = moment('09/02/2018', 'MM/DD/YYYY');
      var disabled_end = moment('09/06/2018', 'MM/DD/YYYY');
      return date.isAfter(disabled_start) && date.isBefore(disabled_end);
    }
  });
});

In the above code, I have loaded multiple scripts on specific page, but I'm unable to initialize those scripts properly.

2
  • 2
    $.getMultiScripts(script_arr) is asynchronous. You can use $.holdReady() or chain .then() to $.getMultiScripts(script_arr) then execute jQuery() within .then() callback. Commented Feb 6, 2019 at 2:56
  • See How can I know when Ajax calls are finished with getScript? Commented Feb 6, 2019 at 3:04

1 Answer 1

1

it is because daterangepicker.js not yet loaded, execute the script in callback or done() function.

if ($(".section").length) {
  var script_arr = [
    'moment.js',
    'daterangepicker.js',
  ];
  $.getMultiScripts(script_arr).done(function() {
    $('#date-picker').daterangepicker({
      "opens": "left",
      singleDatePicker: true,
      isInvalidDate: function(date) {
        var disabled_start = moment('09/02/2018', 'MM/DD/YYYY');
        var disabled_end = moment('09/06/2018', 'MM/DD/YYYY');
        return date.isAfter(disabled_start) && date.isBefore(disabled_end);
      }
    });
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.