Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matty0501/13b2c4156e99935189260d44e04e899b to your computer and use it in GitHub Desktop.
Save matty0501/13b2c4156e99935189260d44e04e899b to your computer and use it in GitHub Desktop.
/**
* Gravity Perks // Nested Forms // Copy Parent Values To First Child Entry
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
*
* Copy parent values into the first child entry automatically. The `{Parent}` merge tag would typically be used
* for this but this would copy the values to all child entries, not just the first.
*/
gform.addAction( 'gpnf_init_nested_form', function( childFormId, gpnf ) {
// Update "123" to your the ID of your child form.
var targetChildFormId = 123;
// Map fields from parent to child in the format
// parent_field_id : child_field_id
var fieldMap = {
'2' : '2',
'3' : '5',
'4_3' : '6_3',
'4_6' : '6_6'
};
if ( childFormId == targetChildFormId && gpnf['entries'].length == 0 ) {
copyParentFormValue( GFFORMID, fieldMap, childFormId );
}
} );
function copyParentFormValue( parentFormId, fieldMap, childFormId ) {
Object.keys(fieldMap).forEach(key => {
var value = jQuery ( '#input_{0}_{1}'.gformFormat( parentFormId, key ) ).val();
// Delaying setting value so Populate Anything can pick up the change event.
setTimeout( function() {
$( '#input_' + childFormId + '_' + fieldMap[key] ).val( value ).change();
} );
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment