sbman12
sbman12

Reputation: 67

flatpickr instance won't open

I have some HTML that uses an instance of flatpickr calendar. What I would like to do is to open up ONLY the specific calendar instance when the accompanying span is clicked.

<div class="formRow">
    <div class="datetimepicker input-group date">
        <input id="initial-notification-date-time" name="initial-notification-date-time" type="text" class="form-control" />
        <span class="input-group-addon"></span>
    </div>
</div>

The accompanying Javascript is:

$('.datetimepicker input').flatpickr({
  dateFormat: 'm/d/Y',
  enableTime: true,
  defaultDate: new Date(),
  onReady: function() {
      var flatPickrInstance = this;
      console.log(flatPickrInstance);
      console.log($(".datetimepicker input").siblings(".datetimepicker span")); 
      $(".datetimepicker input").siblings(".datetimepicker span").click(function () {
          $(".datetimepicker input").flatpickr();
      });
  }
});

What this currently does is cause all instances of ".datetimepicker input" to re-initialize as a new instance. How do I actually cause the instance I want to open?

Upvotes: 3

Views: 9145

Answers (1)

lahendrix
lahendrix

Reputation: 11

You are re-initializing flatpickr each time. You'll need to do something like this:

$('.datetimepicker input').flatpickr({
  dateFormat: 'm/d/Y',
  enableTime: true,
  defaultDate: new Date(),
  onReady: function() {
      var flatPickrInstance = this;
      var $flatPickrInput = $(flatPickrInstance.element);
      $flatPickrInput.siblings("span").click(function () {
          flatPickrInstance.toggle();
      });
  }
});

Upvotes: 1

Related Questions