From ad57040b22f1a5221952116eceb3c0432b73ce45 Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Tue, 7 Oct 2025 10:39:38 +0530 Subject: [PATCH] `gw-auto-add-list-row.js`: Added snippet to automatically add a new last row to a List field. --- gravity-forms/gw-auto-add-list-row.js | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 gravity-forms/gw-auto-add-list-row.js diff --git a/gravity-forms/gw-auto-add-list-row.js b/gravity-forms/gw-auto-add-list-row.js new file mode 100644 index 000000000..22455f4a9 --- /dev/null +++ b/gravity-forms/gw-auto-add-list-row.js @@ -0,0 +1,72 @@ +/** + * Gravity Wiz // Gravity Forms // Auto Add Next List Row + * https://gravitywiz.com/ + * + * Automatically adds a new row to a List field when the user types in the last row. + * + * Instruction Video: https://www.loom.com/share/0120ed39296b4e4988c7fdd054af070e + * + * Instructions: + * + * 1. Install this snippet with our free Code Chest plugin. + * https://gravitywiz.com/gravity-forms-code-chest/ + * + * 2. Adjust the `listSelector` if needed to target a specific List field. + * + * 3. That's it — typing in the last row will automatically create the next one. + */ + +// Selector for the List field container. +// You can refine this selector to target a specific List field, e.g. `.gfield_list_container input[name="input_6[]"]` +var listSelector = '.gfield_list_container'; + +/** + * Bind auto-add behavior to the last row input. + */ +function bindAutoAdd() { + $( listSelector ).each( function() { + var $container = $( this ); + var $lastRow = $container.find( '.gfield_list_group' ).last(); + var $input = $lastRow.find( 'input[type="text"]' ); + + if ( ! $input.length ) { + return; + } + + // Remove old handlers inside this container to avoid duplicates. + $container.find( 'input[type="text"]' ).off( 'input.autoAdd' ); + + // Initialize stored previous value (trimmed). + $input.data( 'lastVal', ( $input.val() || '' ).trim() ); + + // Bind only to the current last input. + $input.on( 'input.autoAdd', function() { + var $this = $( this ); + var prev = $this.data( 'lastVal' ) || ''; + var cur = ( $this.val() || '' ).trim(); + + // Trigger add only when it changed from empty → non-empty. + if ( prev === '' && cur !== '' ) { + var $addButton = $this.closest( '.gfield_list_group' ).find( '.add_list_item' ); + if ( $addButton.length ) { + $addButton.trigger( 'click' ); + } + } + + // Update stored value. + $this.data( 'lastVal', cur ); + } ); + } ); +} + +/** + * Initial binding. + */ +bindAutoAdd(); + +/** + * Rebind after manual "Add" button click. + */ +$( document ).on( 'click', '.add_list_item', function() { + setTimeout( bindAutoAdd, 50 ); +} );