diff --git a/gp-nested-forms/gpnf-add-child-entries-to-the-top.js b/gp-nested-forms/gpnf-add-child-entries-to-the-top.js new file mode 100644 index 000000000..beab3d783 --- /dev/null +++ b/gp-nested-forms/gpnf-add-child-entries-to-the-top.js @@ -0,0 +1,26 @@ +/** + * Gravity Perks // Nested Forms // Subscribe to Child Entry Updates + * https://gravitywiz.com/documentation/gravity-forms-nested-forms/ + * + * Experimental Snippet 🧪 + */ +// Get your Nested Forms JavaScript instance where "4" is the Nested Form field ID. +var gpnf = window.GPNestedForms_GFFORMID_13; +var entryCount = 0; +var doingItLive = false; + +gpnf.viewModel.entries.subscribe( function( entries ) { + if ( ! doingItLive ) { + entryCount = entries.length; + } +}, null, 'beforeChange' ); + +gpnf.viewModel.entries.subscribe( function( entries ) { + // Check if an entry was added. + if ( ! doingItLive && entries.length > entryCount ) { + doingItLive = true; + gpnf.viewModel.entries.splice( 0, 0, gpnf.viewModel.entries.pop() ); + entryCount = entries.length; + doingItLive = false; + } +} );