Created
May 20, 2025 10:28
-
-
Save saifsultanc/d644b4fe35a55566b6f38172119990dc to your computer and use it in GitHub Desktop.
gpld-populate-new-minimum-date-into-linked-date-field.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sourceFieldId = 37; // Replace with the ID of the source field (Field A) | |
// Track completion of required events | |
let postRenderComplete = false; | |
let minDateComplete = false; | |
let formId; | |
let calculationTimeout; | |
function checkAndRunCalculations() { | |
if (postRenderComplete && minDateComplete) { | |
// Reset for future checks. | |
postRenderComplete = false; | |
minDateComplete = false; | |
// Trigger calculations with delay. | |
debouncedTriggerCalculations(); | |
} | |
} | |
function triggerCalculations() { | |
if (!formId) return; | |
// Clear any pending calculation. | |
clearTimeout(calculationTimeout); | |
// Trigger calculations by simulating a conditional logic change. | |
jQuery(document).trigger(`gform_post_conditional_logic.gfCalc_${formId}`); | |
// Also trigger change on calculation fields to ensure they update. | |
jQuery(`#gform_${formId} .gfield_calculation`).find('input').trigger('change'); | |
} | |
// Debounced trigger calculations. | |
const debouncedTriggerCalculations = () => { | |
clearTimeout(calculationTimeout); | |
calculationTimeout = setTimeout(triggerCalculations, 300); | |
}; | |
// Initialize field events on form render. | |
document.addEventListener('gform/post_render', (event) => { | |
formId = event.detail.formId; | |
const $field = getSourceField(formId, sourceFieldId); | |
// Set up event listeners for the source field with debounce. | |
$field.on('input change', function() { | |
debouncedTriggerCalculations(); | |
}); | |
triggerFieldEventsIfValueExists($field); | |
postRenderComplete = true; | |
checkAndRunCalculations(); | |
}); | |
// Handle conditional logic changes. | |
gform.addAction( | |
'gform_post_conditional_logic_field_action', | |
(formId, action, targetId, defaultValues, isInit) => { | |
if (action == 'hide' || targetId == '#gform_submit_button_' + formId) { | |
return; | |
} | |
const $field = getSourceField(formId, sourceFieldId); | |
triggerFieldEventsIfValueExists($field); | |
} | |
); | |
// Handle min date setting completion. | |
gform.addAction('gpld_after_set_min_date', function($input, date) { | |
$input.datepicker('setDate', date); | |
minDateComplete = true; | |
checkAndRunCalculations(); | |
}); | |
// Triggers input and change events on a field if it has a value. | |
const triggerFieldEventsIfValueExists = ($field) => { | |
const value = $field.val(); | |
if (value) { | |
requestAnimationFrame(() => { | |
$field.trigger('input').trigger('change'); | |
}); | |
} | |
}; | |
// Get the source field based on form ID and field ID. | |
const getSourceField = (formId, fieldId) => { | |
return jQuery(`#input_${formId}_${fieldId}`); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment