Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 317
sbman12 Avatar asked Oct 24 '25 15:10

sbman12


1 Answers

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();
      });
  }
});
like image 104
lahendrix Avatar answered Oct 26 '25 04:10

lahendrix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!