|
10 | 10 | * 1. Install this snippet with our free Custom JavaScript plugin.
|
11 | 11 | * https://gravitywiz.com/gravity-forms-code-chest/
|
12 | 12 | */
|
13 |
| - gform.addAction( 'gpld_after_set_min_date', function( $input, date ) { |
14 |
| - $input.datepicker( 'setDate', date ); |
| 13 | +const sourceFieldId = 25; // Replace with the ID of the source field (Field A) |
| 14 | + |
| 15 | +// Initialize field events on form render. |
| 16 | +document.addEventListener( 'gform/post_render', (event) => { |
| 17 | + const formId = event.detail.formId; |
| 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 | + // Only for fields and when we are not hiding them. |
| 27 | + if (action == 'hide' || targetId == '#gform_submit_button_' + formId) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const $field = getSourceField(formId, sourceFieldId); |
| 32 | + triggerFieldEventsIfValueExists($field); |
| 33 | + } |
| 34 | +); |
| 35 | + |
| 36 | +// Triggers input and change events on a field if it has a value. |
| 37 | +const triggerFieldEventsIfValueExists = ($field) => { |
| 38 | + const value = $field.val(); |
| 39 | + if (value) { |
| 40 | + requestAnimationFrame(() => { |
| 41 | + $field.trigger('input').trigger('change'); |
| 42 | + }); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +// Get the source field based on form ID and field ID. |
| 47 | +const getSourceField = (formId, fieldId) => { |
| 48 | + return jQuery(`#input_${formId}_${fieldId}`); |
| 49 | +}; |
| 50 | + |
| 51 | +gform.addAction( 'gpld_after_set_min_date', function( $input, date ) { |
| 52 | + $input.datepicker( 'setDate', date ); |
15 | 53 | } );
|
0 commit comments