Utilizing the Flatpickr's onChange event to dynamically update the end date

I am utilizing two date pickers, start_time and end_time, both implemented with the flatpickr() function. When a date is selected for the start_time, I want the end_time to automatically update to match that value.

To achieve this functionality, I am attempting to make use of the onChange hook which is detailed in the documentation available at .

Date Picker Forms

<div class="start_time_result mb-6" style ="width: 30vw;">
  <%= form.label :start_time, class: 'label' %>
  <div class="flex items-center justify-between max-w-md">
    <%= form.text_field :start_time, data: { behavior: "flatpickr" }, placeholder: "Date and time select ...", class: "form-control" %>
  </div>
</div>
<div class="field" style ="width: 30vw;">
  <%= form.label :end_time, class: 'label' %>
  <div class="end_time_result flex items-center justify-between max-w-md">
    <%= form.text_field :end_time, data: { behavior: "flatpickr" }, placeholder: "Date and time select ...", class: "form-control required" %>
  </div>
</div>

Javascript Code Block

document.addEventListener('turbolinks:load', function() {
  document.querySelector('#recurring_event a')
    .addEventListener("ajax:success", function(data, status, xhr) {
      flatpickr("[data-behavior='flatpickr']", {
        enableTime: false,
        altInput: true,
        altFormat: "F j, Y",
        minDate: "today",
        onChange: function(dateObj, dateStr) {
          end_time.set('minDate', dateStr)
        }
      })
    });
});

Answer №1

You can utilize the onChange event of #start_time to set the minimum date (minDate) for end_time.

Here is an example:

$(document).ready(function() {
  let start_time = $('#start_time');
  let end_time = $('#end_time');

  start_time.flatpickr({
    altInput: true,
    altFormat: "F j, Y",
    onChange: function(selectedDates) {
      end_time.flatpickr({
        altInput: true,
        altFormat: "F j, Y",
        minDate: new Date(selectedDates),
      });
    }
  });

  end_time.flatpickr({});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.11/flatpickr.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.11/flatpickr.min.js"></script>

<input id="start_time">
<input id="end_time">

Answer №2

After encountering the same challenge, I finally managed to make it work just half an hour ago. My approach involved using a Stimulus controller in a Rails 6 application with the Flatpickr package integrated through Webpacker.

Since my flatpickr instances were used globally on the site, I handled instantiation in the application js file:

//application.js
import { Application } from "@hotwired/stimulus";
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers";
import flatpickr from "flatpickr";
window.Stimulus = Application.start()
Stimulus.load(definitionsFromContext(context))

document.addEventListener("turbo:load", () => {
  flatpickr("[data-datetimepicker='flatpickr']", {
    enableTime: true, 
    dateFormat: "Z",
    minDate: "today",
    altInput: true,
    altFormat: "M. j, Y h:i K", 
    allowInput: true, 
    onOpen: function(selectedDates, dateStr, instance) {
        $(instance.altInput).prop('readonly', true);
    },
    onClose: function(selectedDates, dateStr, instance) {
        $(instance.altInput).prop('readonly', false);
        $(instance.altInput).blur();
    },
  })
})

Both my start_time and end_time inputs utilized the same flatpickr instances (not ideal, I know), causing JavaScript errors in the console when trying to update the onChange configuration of the start_time input post turbo:load. Specifically, the start_time instance was undefined when attempting another turbo:load event to replicate the datetime value, resulting in a console error of 'Uncaught TypeError (Cannot read properties of undefined)'.

The Turbo handbook provided essential guidance that led me to the solution: "When possible, avoid attaching additional event listeners directly to elements on the page body using the turbo:load event." Hence, I developed a Stimulus controller to duplicate the flatpickr dateObj value from one instance to another:

//dup_datetime_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["mirror"]
  
  dup(event){
     let origin = event.target._flatpickr
     let mirror = this.mirrorTarget._flatpickr
     
     origin.config.onChange.push(function(dateObj) {
        mirror.set("minDate", dateObj)
        mirror.setDate(dateObj)
     })
  }
}

...inserted the controller reference into the containing div of the input elements:

//_form.html.erb
<div class="card-body" data-controller="dup-datetime">

...included a data-action attribute for the start_time input:

//_form.html.erb
<%= form_with(model: @ask) do |f| %>
<%= f.text_field :start_time, required: true, data: { datetimepicker: "flatpickr", action: "input->dup-datetime#dup" }, class: 'form-control bg-white', id: "quick_starttime", readonly:'readonly' %>

...and added the Stimulus target data attribute to the end_time input:

//_form.html.erb
<%= f.text_field :end_time, required: true, data: { datetimepicker: "flatpickr", dup_datetime_target: "mirror" }, class: 'form-control bg-white', id: "quick_endtime", readonly:'readonly' %>

I hope this solution spares someone else several hours (or even days... weeks...) of their life.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What does the error message "unsupported command-line flag" in Chrome mean for Protractor?

Recently jumping on the Protractor bandwagon, I found myself facing an issue while running my tests on Chrome (an error message appears below the address bar in the browser): A worrisome warning popped up saying something about '--ignore-certificat ...

Coding with a combination of JavaScript, AngularJS, and manipulating brackets

I am currently performing the following action: myArray.push(pageCount); After that, I end up with something like this: $scope.myArray = Pages.getAllPageCount(); Finally, when I utilize AngularJS to display it in my .html file. {{myArray}} If there i ...

Exiting or returning from a $scope function in AngularJS: a guide

There are times when I need to exit from my $scope function based on a certain condition. I have attempted to achieve this using the return statement. However, my efforts have been in vain as it only exits from the current loop and not from the main scop ...

How to display a modal within a router-link in Vue 3?

Below are buttons with router-links. However, I only want the calculator button to open a modal. When I execute the code provided, all buttons trigger the modal instead of just the calculator button. Output: Router-link Code: <div class="contai ...

Is it possible to duplicate the mesh thousands of times and animate it without causing significant performance issues

I have created a demo with numerous cubes that share the same geometry and texture: texture = THREE.ImageUtils.loadTexture ... material = new THREE.MeshLambertMaterial( map: texture ) geometry = new THREE.BoxGeometry( 1, 1, 1 ) cubes = [] for i in [0..1 ...

Programmatically searching individual columns in Datatables is a powerful feature that

I am currently working on creating a jQuery datatable with search functionality in each column, using the example provided on the datatables page found at https://datatables.net/examples/api/multi_filter.html Specifically, I want to be able to search the ...

jQuery encountering error when uploading multiple files upon loading

I'm having trouble with this jQuery plugin ( ). Whenever I try to set the events on it, I keep getting an error message saying "Function expected". Can someone provide assistance? Everything seems to be working fine except for binding to the events. ...

What is the process for retrieving an object from a node.js server using JSON.stringify() in a JavaScript file?

Looking to organize my code, I want to separate the JavaScript from my page and link it through a file instead of having it embedded within script tags. The issue arises when I receive a "SyntaxError: expected expression, got '<' " in Firefox ...

What is the accurate way to write the ID selector for the select-option-selected in JQuery?

When it comes to extracting a value from a Select-Option using jQuery, the following syntax can be used. I have successfully retrieved data using this method. $( "#Vienna\\.rail0\\.track option:selected" ).text() However, there is an ...

In React, the clearInterval() function does not effectively clear intervals

Currently, I am implementing the following code: startInterval = () => { this.interval = setInterval(this.intervalFunction, 10000) } stopInterval = () => { clearInterval(this.interval) } However, a problem arises when I invoke the stopInte ...

Neither Output nor EventEmitter are transmitting data

I am struggling to pass data from my child component to my parent component using the Output() and EventEmitter methods. Despite the fact that the emitter function in the child component is being called, it seems like no data is actually being sent through ...

Identify and sort JSON objects based on keys with multiple values

My JSON file contains objects structured like this: [ { "name" : "something", "brand": "x", "category" : "cars" }, { "name" : "something2 ...

Tips for blocking submissions when a user tries to input a hyperlink

I have encountered a problem in my journey of learning JS and unfortunately, I couldn't resolve it by myself. Lately, spam has been flooding through the form on my website and all my attempts with jQuery and JS to fix it have failed. As a last resort ...

An issue arose upon scanning the QR code for whatsapp-web.js, indicating a potential

My WhatsApp web bot encountered issues this week. C:\pinkmeupwabot\node_modules\whatsapp-web.js\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221 throw new Error('Eval ...

Is it possible to assign a class to the initial word within a designated class?

How can I customize the first and second word of a specific class in my code? This is an example of my current code: <h2 class="content-heading">Latest News</h2> I would like to achieve something similar to this: <h2 class="content-headi ...

Is it better to use Asynchronous or Synchronous request with XMLHttpRequest in the Firefox Extension for handling multiple requests

As a newcomer to developing Firefox Add-Ons, my initial project involves calling an external API in two steps: Step 1) Retrieve data from the API. Step 2) Use the data retrieved in Step 1 to make another call to the same API for additional information. ...

Access data from JSON array in Angular 2

I'm facing a basic issue here. I have a JSON file named pageDefinition.json that is being loaded into my component. Here's how the JSON data looks: ... "testArray": [ {"id": 0, "name": "row1"}, {"id": 1, "name": "row2"}, {"id": 2, "n ...

Utilizing asynchronous programming for scenarios where two requests need to be sent, with the response from the first request being required for the second request

I have an async function that looks like this: exports.myFunction = async (req, res, next) => { if (some condition) { next() } try { const results = await axios.get(`https://a-domain.com/url/path`); const info = results.data; c ...

Combining arrays using the jQuery .each() function

I'm attempting to generate an array from several div id's. Check out my code below: $(function(){ $('#content > div[id^=post]').each(function(){ var ele = Number($(this).attr('id').substr(5,4)); var arr ...

Filtering rows of an HTML table that contain links in the `<href>` column data in real time

When using HTML and JavaScript, I have found a solution that works well for many of the columns I am working with. You can see the details on how to dynamically filter rows of an HTML table using JavaScript here. var filters=['hide_broj_pu',&apo ...