Skip to content

Commit 59cc849

Browse files
committed
gpld-populate-new-minimum-date-into-linked-date-field.js: Fixed an issue with default value of field notgetting used to apply minimum date.
1 parent 8f8a375 commit 59cc849

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

gp-limit-dates/gpld-populate-new-minimum-date-into-linked-date-field.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,37 @@
1111
* https://gravitywiz.com/gravity-forms-code-chest/
1212
*/
1313
const sourceFieldId = 25; // Replace with the ID of the source field (Field A)
14-
document.addEventListener( 'gform/post_render', ( event ) => {
15-
const $field = jQuery( `#input_GFFORMID_${sourceFieldId}` );
16-
const value = $field.val();
17-
if ( value ) {
18-
requestAnimationFrame( function(){
19-
$field.trigger( 'input' ).trigger( 'change' );
14+
15+
// Initialize field events on form render.
16+
document.addEventListener( 'gform/post_render', (event) => {
17+
const formId = event.detail.formId; // Get the form ID from the event
18+
const $field = getSourceField(formId, sourceFieldId);
19+
triggerFieldEventsIfValueExists($field);
20+
});
21+
22+
// Handle conditional logic changes.
23+
gform.addAction(
24+
'gform_post_conditional_logic_field_action',
25+
(formId, action, targetId, defaultValues, isInit) => {
26+
const $field = getSourceField(formId, sourceFieldId);
27+
triggerFieldEventsIfValueExists($field);
28+
}
29+
);
30+
31+
// Triggers input and change events on a field if it has a value.
32+
const triggerFieldEventsIfValueExists = ($field) => {
33+
const value = $field.val();
34+
if (value) {
35+
requestAnimationFrame(() => {
36+
$field.trigger('input').trigger('change');
2037
});
2138
}
22-
});
39+
};
40+
41+
// Get the source field based on form ID and field ID.
42+
const getSourceField = (formId, fieldId) => {
43+
return jQuery(`#input_${formId}_${fieldId}`);
44+
};
2345

2446
gform.addAction( 'gpld_after_set_min_date', function( $input, date ) {
2547
$input.datepicker( 'setDate', date );

0 commit comments

Comments
 (0)