Remove Empty Choices

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/

Code

Filename: gppa-remove-empty-choices.js

/**
 * Gravity Perks // Populate Anything // Remove Empty Choices
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 */
var $containers = $( '.gfield--type-choice' );

// On page load.
$containers.each( function() {
	gwizRemoveEmptyChoices( $( this ) );
} );

// Show/hide checkboxes when the field is manipulated.
$containers.bind( 'DOMNodeInserted DOMNodeRemoved', function() {
	gwizRemoveEmptyChoices( $( this ) );
} );

function gwizRemoveEmptyChoices( $elem ) {
	$elem.find( 'input[type="checkbox"], input[type="radio"]' ).each( function() {
		if( ! $( this ).val() ) {
			$( this ).parent().hide();
		}
		else {
			$( this ).parent().show();
		}
	} );
}

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.