Description
The gform_post_conditional_logic_field_action JavaScript action allows developers to perform custom actions when a field is displayed or hidden by conditional logic on the frontend.
Usage
gform.addAction('gform_post_conditional_logic_field_action', function (formId, action, targetId, defaultValues, isInit) {
// do stuff
});
Parameters
| Parameter | Type | Description |
|---|---|---|
formId | integer | The ID of the current form. |
action | string | The current conditional logic action. Possible values: show or hide. |
targetId | string | The ID selector for the field container (li) in the format #field_{formId}_{fieldId} e.g. #field_10_2. |
defaultValues | string | array | The field default values. |
isInit | boolean | True when the form is initially displayed, otherwise false. |
Examples
Reset Coupon field.
The following example shows how you can reset the coupon field when hidden by conditional logic.
gform.addAction('gform_post_conditional_logic_field_action', function (formId, action, targetId, defaultValues, isInit) {
if (!isInit && action == 'hide') {
var target = jQuery(targetId),
coupon_items = target.find('tr.gf_coupon_item');
if (coupon_items.length) {
coupon_items.remove();
target.find('input[type=hidden]').val('').change();
}
}
});
Show/Hide the Save & Continue Link.
This example shows how conditional logic can be used to show and hide the save and continue link at the same time as another field, in this case field 2 of form id 616.
gform.addAction('gform_post_conditional_logic_field_action', function (formId, action, targetId, defaultValues, isInit) {
if (targetId == '#field_616_2') {
if (action == 'show') {
jQuery('#gform_save_616_footer_link').show();
} else {
jQuery('#gform_save_616_footer_link').hide();
}
}
});
Placement
Reference the article Adding JavaScript Code to the Frontend of Your Site.
Source Code
This hook is located in js/conditional_logic.js.