gform_post_conditional_logic_field_action

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

ParameterTypeDescription
formIdintegerThe ID of the current form.
actionstringThe current conditional logic action. Possible values: show or hide.
targetIdstringThe ID selector for the field container (li) in the format #field_{formId}_{fieldId} e.g. #field_10_2.
defaultValuesstring | arrayThe field default values.
isInitbooleanTrue 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();
		}
	}
});

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.