Preserve Field Value on Conditional Logic Hide
Instructions
- Install our free Gravity Forms Custom Javascript plugin. https://gravitywiz.com/gravity-forms-code-chest/
- Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
- Update ‘4’ to the ID of the field you want to target.
Code
Filename: gw-preserve-field-value-on-hide.js
/**
* Gravity Wiz // Gravity Forms // Preserve Field Value on Conditional Logic Hide
* https://gravitywiz.com/
*
* Instructions:
* 1. Install our free Gravity Forms Custom Javascript plugin.
* https://gravitywiz.com/gravity-forms-code-chest/
* 2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
* 3. Update '4' to the ID of the field you want to target.
*/
gform.addFilter( 'gform_reset_pre_conditional_logic_field_action', function( shouldReset, formId, targetId, defaultValues, isInit ) {
if ( formId == GFFORMID && gf_get_input_id_by_html_id( targetId ) == 4 ) {
shouldReset = false;
}
return shouldReset;
} );
I would like to apply this filter to a series of fields. I modified the function (gist here: https://gist.github.com/goatlady/e32995088b18a3497c17f87a7816b240) to add an array of targetIds to check against, but it’s not applying and I can’t figure out why. Any help greatly appreciated :)
I’m using the Code Chest plugin and the original version of the snippet which has the targetId hard-coded does work.
Hi Kay,
I’ve modified the snippet to allow for an array of field IDs. You can find the customized snippet here: https://gist.github.com/SebastianWiz/3b3b7f5e34988da6b385cd53fe300a03
Best, Sebastian
Thanks Sebastian! The only functional difference between your code and my code that I can see is the “var” scoping, but I still can’t get it working: I made a small sample form with three fields hidden by a checkbox and pasting the original function in three times works as expected, whereas using just your new version with the three id numbers does not.
What’s the easiest way to share what I’ve done so that you can see it? I’m using the latest versions of WP and all plugins.
Hi Kay,
Can you wrap the values within the array in quotes as seen below and see how it works
targetIds = ["8","15","20","26","30","36","39","45","48","51"];
? If it’s still not working for you, you can submit a ticket for this via our support form so we can take a closer look at your setup and assist you better.Best,
Amazing – adding quotes worked. Thanks so much!
Hi Kay,
You’re welcome. Glad we could help.
Best,