Skip to content

Instantly share code, notes, and snippets.

@dexit
Last active November 5, 2025 17:04
Show Gist options
  • Select an option

  • Save dexit/d0c003e6034c2e8a377ed25b1160e5a1 to your computer and use it in GitHub Desktop.

Select an option

Save dexit/d0c003e6034c2e8a377ed25b1160e5a1 to your computer and use it in GitHub Desktop.
This is used with combinatin of yeeAddons date, prepare teh dd/mm/yyyy input visually but passed "yyyy-mm-dd" on submit to Hubspot in this case. Accepts Keydown (enter) and leaves it be meaning allows it.
jQuery(document).ready(function ($) {
/* ------------------------------------------------------------------
* 1. Flatpickr options
* ------------------------------------------------------------------ */
const flatpickrOptions = {
// Format that will be written to the hidden input (HubSpot)
dateFormat: "Y-m-d",
// Format that the user sees / types
altFormat: "d/m/Y",
mode: "single",
allowInput: true, // let the user type
altInput: true, // create a visible altInput
// Minimum date that the picker will allow
minDate: "1900-01-01",
maxDate: "",
/* ------------------------------------------------------------------
* 2. Detect manual typing
* ------------------------------------------------------------------ */
// When the user presses a key in the visible input, set a flag
onKeyDown: function (e) {
//e.target === this.input //(the visible altInput)
this.isTyping = true;
},
/* ------------------------------------------------------------------
* 3. Handle changes – differentiate between typing & calendar
* ------------------------------------------------------------------ */
onChange: function (selectedDates, dateStr, instance) {
// `dateStr` is the string typed by the user (if any)
// `selectedDates[0]` is a Date object (if a date was chosen)
if (instance.isTyping) {
/* ---- Manual input ---- */
// Validate the format strictly (dd/mm/yyyy)
const m = moment(dateStr, "DD/MM/YYYY", true); // true = strict
if (!m.isValid()) {
// Show an error and clear the visible field
console.log("Invalid date format. Please use dd/mm/yyyy");
// Clear the visible input – use the public API
// instance.setDate(null, false); // clears the visible field
// The hidden field stays unchanged
} else {
// Valid manual input – you can run any custom logic here
console.log("User typed a valid date:", m.format("YYYY-MM-DD"));
// The hidden field will automatically be updated to YYYY-MM-DD
}
} else {
/* ---- Calendar selection ---- */
// `selectedDates[0]` is a Date object
console.log("User selected via calendar:", selectedDates[0].toISOString().slice(0, 10));
// The hidden field will automatically be updated to YYYY-MM-DD
}
// Reset the flag – next change will be considered a new action
instance.isTyping = false;
},
/* ------------------------------------------------------------------
* 4. Clean up after the picker closes
* ------------------------------------------------------------------ */
onClose: function () {
this.isTyping = false;
}
};
/* ------------------------------------------------------------------
* 5. Initialise Flatpickr on the hidden input
* ------------------------------------------------------------------ */
setTimeout(function () {
// The hidden input that holds the Y‑m‑d value
const $hiddenInput = $("#form-field-date_of_birth__date_");
if ($hiddenInput.length) {
// Initialise Flatpickr on the hidden input
flatpickr($hiddenInput[0], flatpickrOptions);
/* ------------------------------------------------------------------
* 6. Hide Elementor's pre‑existing visible input
* ------------------------------------------------------------------ */
// Elementor often creates a visible <input type="text"> right after the hidden one.
// We find that sibling and hide it so the user only sees the Flatpickr altInput.
const $elemVisible = $hiddenInput
.nextAll('input[type="text"].elementor-date-field')
.first();
if ($elemVisible.length) {
// $elemVisible.hide(); // hide the Elementor field
$elemVisible.removeAttr("required"); // optional – let Flatpickr handle validation
}
}
}, 1000); // 1‑second delay – adjust if Elementor needs more time
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment