Copy Parent Value Manually

Manually copy a parent value into a child form field. The {Parent} merge tag would typically be used for this but when you are embedding a child form into multiple parent forms, the {Parent} merge tag cannot differentiate between them and will populate incorrect values.

Code

Filename: gpnf-copy-parent-value-manually.js

/**
 * Gravity Perks // Nested Forms // Copy Parent Value Manually
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Manually copy a parent value into a child form field. The `{Parent}` merge tag would typically be used
 * for this but when you are embedding a child form into multiple parent forms, the `{Parent}` merge tag
 * cannot differentiate between them and will populate incorrect values.
 */
gform.addAction( 'gpnf_init_nested_form', function( childFormId, gpnf ) {

	// Update "123" to your the ID of your child form.
	var targetChildFormId = 123;

	// Update "4" to the ID of the child field in which the parent form ID will be populated.
	var childFieldId = 4;

	// Update "5" to the ID of the parent field whose value will be copied to the child field.
	var parentFieldId = 5;

	if ( childFormId == targetChildFormId ) {
		copyParentFormValue( GFFORMID, parentFieldId, childFormId, childFieldId );
	}

} );

function copyParentFormValue( parentFormId, parentFieldId, childFormId, childFieldId ) {

	var value = jQuery ( '#input_{0}_{1}'.gformFormat( parentFormId, parentFieldId ) ).val() ||
		jQuery('input[name="input_{0}"]:checked'.gformFormat( parentFieldId )).val() ||
		jQuery('input[name="input_{0}"]:selected'.gformFormat( parentFieldId )).val();

	// Delaying setting value so Populate Anything can pick up the change event.
	setTimeout( function() {
		$( '#input_' + childFormId + '_' + childFieldId ).val( value ).change();
	} );

}

Comments

  1. Lutz Philipp
    Lutz Philipp August 11, 2024 at 10:52 am

    I get a Syntax error: unexpected token “,”, expecting variable in line:

    gform.addAction( ‘gpnf_init_nested_form’, function( childFormId, gpnf ) {

    Reply
    1. Roxy Stoltz
      Roxy Stoltz Staff August 12, 2024 at 4:33 am

      Hey Lutz,

      This error is typically caused when a JS snippet is added as PHP. Since this is a JavaScript snippet, can you try adding the code directly to the form using our free Gravity Forms Code Chest plugin and see if this resolves the issue? If that makes no difference, we’d need to take a closer look at the current configuration to see what’s going on here. If you have an active Gravity Perks License, you can get in touch with us via our support form so we can dig into this.

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.