Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions gravity-forms/gw-choice-counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Get the total number of checkboxes checked or multi-select options selected. Useful when wanting to apply conditional
* logic based on those totals.
*
* @version 1.1
* @version 1.2
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/
Expand Down Expand Up @@ -75,7 +75,7 @@ function output_script() {
$parentForm.off( 'click', choiceFieldSelector, self.updateChoiceEventHander );
$parentForm.off( 'change', choiceFieldSelector, self.updateChoiceEventHander );

if ( self.isCheckboxField( $choiceField ) ) {
if ( self.isCheckableField( $choiceField ) ) {
$parentForm.on( 'click', choiceFieldSelector, self.updateChoiceEventHandler );
} else {
$parentForm.on( 'change', choiceFieldSelector, self.updateChoiceEventHandler );
Expand Down Expand Up @@ -104,8 +104,8 @@ function output_script() {
} );
};

self.isCheckboxField = function( $field ) {
return Boolean( $field.find( 'input[type="checkbox"]' ).length );
self.isCheckableField = function($field ) {
return Boolean( $field.find( ':checkbox, :radio' ).length );
}

self.updateChoiceCount = function( formId, choiceFieldIds, countFieldId, values ) {
Expand All @@ -118,15 +118,15 @@ function output_script() {
var $choiceField = $( '#input_' + formId + '_' + choiceFieldIds[ i ] );
if ( ! values ) {
// If no values provided in the config, just get the number of checkboxes checked.
if ( self.isCheckboxField( $choiceField ) ) {
count += $choiceField.find( 'input[type="checkbox"]:checked' ).not(' #choice_' + choiceFieldIds[ i ] + '_select_all').length;
if ( self.isCheckableField( $choiceField ) ) {
count += $choiceField.find( ':checked' ).not(' #choice_' + choiceFieldIds[ i ] + '_select_all').length;
} else {
count += $choiceField.find( 'option:selected' ).length;
}
} else {
// When values are provided, match the values before adding them to count.
var selectedValues = [];
$choiceField.find( 'input[type="checkbox"]:checked' ).each( function( k, $selectedChoice ) {
$choiceField.find( ':checked' ).each( function( k, $selectedChoice ) {
selectedValues.push( $selectedChoice.value );
});
values.forEach( function( val ) {
Expand Down