Skip to content

Instantly share code, notes, and snippets.

@samjco
Forked from ensostyle/ListFieldRowTotal
Created January 27, 2022 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samjco/a70e7eaa815157eee05d92c31cf09978 to your computer and use it in GitHub Desktop.
Save samjco/a70e7eaa815157eee05d92c31cf09978 to your computer and use it in GitHub Desktop.
Count the number of rows in a Gravity Forms list field and store the result in another field.
<script>
function ListFieldRowCount( listField, totalField ) {
var totalRows = jQuery( listField ).find('table.gfield_list tbody tr').length;
jQuery( totalField ).val( totalRows ).change();
}
function ListFieldRowTotal( formId, fieldId, totalFieldId ) {
var listField = '#field_' + formId + '_' + fieldId;
var totalField = '#input_' + formId + '_' + totalFieldId;
ListFieldRowCount( listField, totalField );
jQuery( listField ).on( 'click', '.add_list_item', function() {
ListFieldRowCount( listField, totalField );
jQuery( listField + ' .delete_list_item' ).removeProp( 'onclick' );
});
jQuery( listField ).on( 'click', '.delete_list_item', function() {
gformDeleteListItem( this, 0 );
ListFieldRowCount( listField, totalField );
});
}
ListFieldRowTotal( 158, 1, 7 );
//ListFieldColumnTotal( form id, list field id, result - field id );
// enable dynamic population on field that is to hold result
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment