Preserve Field Value on Conditional Logic Hide

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.

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;
} );

Comments

    1. Kay Smoljak
      Kay Smoljak August 30, 2024 at 5:05 am

      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.

    2. Samuel Bassah
      Samuel Bassah Staff August 30, 2024 at 7:39 am

      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,

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.