Auto Add Next List Row

Automatically adds a new row to a List field when the user types in the last row.

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.

Code

Filename: gw-auto-add-list-row.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 );
} );

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.