# Counting custom field checkboxes and input the sum to product quantity

**URL:** <https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631>\
**Category:** Get Help\
**Tags:** count, checkbox-count, gravity-wiz\
**Created:** [August 2, 2023, 9:26am UTC](https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631 "2023-08-02T09:26:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![icomputedata](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@icomputedata](https://community.gravityforms.com/u/icomputedata)\
**Post date:** [August 2, 2023, 9:26am UTC](https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631/1 "2023-08-02T09:26:04Z")

</div>

Hi,  
Non coder here.  
I used chatGPT to generate code below

jQuery(document).ready(function($) {  
// Replace ‘input\_{GFFORMID}_40’ with the actual ID of your checkbox field  
// Replace 'input_{GFFORMID}_44’ with the actual ID of your quantity field  
const checkboxField = $('input#input_{GFFORMID}_40[type=“checkbox”]');  
const quantityField = $('input#input_{GFFORMID}\_44[type=“number”]’);

checkboxField.on(‘change’, function() {  
let totalSum = 0;  
checkboxField.each(function() {  
if ($(this).is(‘:checked’)) {  
const checkboxValue = parseFloat($(this).val());  
if (!isNaN(checkboxValue)) {  
totalSum += checkboxValue;  
}  
}  
});  
quantityField.val(totalSum);  
});  
});

I use Gravity WIZ Custom Javascript plugin for implementing this.  
Kindly help me to rectify the error.

---

<div class="post-metadata">

**Author:** ![samuel](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/samuel/32/2539_2.png) [@samuel](https://community.gravityforms.com/u/samuel)\
**Post date:** [August 2, 2023, 12:55pm UTC](https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631/2 "2023-08-02T12:55:54Z")

</div>

Hi Compute,

I am unsure about your Javascript snippet, but looking at what you’re trying to do, I think our [Choice Counter snippet](https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-choice-counter.php) will work for you. Here’s the [documentation](https://gravitywiz.com/count-the-total-number-of-checked-checkboxes-with-gravity-forms/) on how to use the snippet and how it works.

> **[Count the Total Number of Checked Checkboxes with Gravity Forms - Gravity Wiz](https://gravitywiz.com/count-the-total-number-of-checked-checkboxes-with-gravity-forms/)**
>
> This snippet gets the total number of checked Checkboxes and dynamically populates that total into a Number field on the form.

---

<div class="post-metadata">

**Author:** ![icomputedata](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@icomputedata](https://community.gravityforms.com/u/icomputedata)\
**Post date:** [August 3, 2023, 8:02am UTC](https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631/3 "2023-08-03T08:02:17Z")

</div>

Thanks it is working now yet i am facing another issue.  
I tried to populate it to 2 different count field from the same checkbox but it is neglecting the other count field.

**Trial-1**

# Configuration

new GW\_Choice\_Count( array(  
‘form\_id’ =\> 1, // The ID of your form.  
‘count\_field\_id’ =\> 44, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.  
‘choice\_field\_ids’ =\> array( 40 ), // Any array of Checkbox or Multi-select field IDs which should be counted.  
‘values’ =\> false, // Specify an array of values that should be counted. Values not in this list will not be counted. Defaults to `false` which will count all values.  
) );

new GW\_Choice\_Count( array(  
‘form\_id’ =\> 1, // The ID of your form.  
‘count\_field\_id’ =\> 46, 54, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.  
‘choice\_field\_ids’ =\> array( 45 ), // Any array of Checkbox or Multi-select field IDs which should be counted.  
‘values’ =\> false, // Specify an array of values that should be counted. Values not in this list will not be counted. Defaults to `false` which will count all values.  
) );

**Trial -2**

# Configuration

new GW\_Choice\_Count( array(  
‘form\_id’ =\> 1, // The ID of your form.  
‘count\_field\_id’ =\> 44, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.  
‘choice\_field\_ids’ =\> array( 40 ), // Any array of Checkbox or Multi-select field IDs which should be counted.  
‘values’ =\> false, // Specify an array of values that should be counted. Values not in this list will not be counted. Defaults to `false` which will count all values.  
) );

new GW\_Choice\_Count( array(  
‘form\_id’ =\> 1, // The ID of your form.  
‘count\_field\_id’ =\> 46, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.  
‘choice\_field\_ids’ =\> array( 45 ), // Any array of Checkbox or Multi-select field IDs which should be counted.  
‘values’ =\> false, // Specify an array of values that should be counted. Values not in this list will not be counted. Defaults to `false` which will count all values.  
) );

new GW\_Choice\_Count( array(  
‘form\_id’ =\> 1, // The ID of your form.  
‘count\_field\_id’ =\> 54, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.  
‘choice\_field\_ids’ =\> array( 45 ), // Any array of Checkbox or Multi-select field IDs which should be counted.  
‘values’ =\> false, // Specify an array of values that should be counted. Values not in this list will not be counted. Defaults to `false` which will count all values.  
) );

for both of the above trials either one count field is working.  
Kindly help to rectify the error.

---

<div class="post-metadata">

**Author:** ![samuel](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/samuel/32/2539_2.png) [@samuel](https://community.gravityforms.com/u/samuel)\
**Post date:** [August 3, 2023, 9:22am UTC](https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631/4 "2023-08-03T09:22:18Z")

</div>

Hi,

Trial two should work. However, if it’s still not working for you, you’ll need to contact us via our [support form](https://gravitywiz.com/support/) so we can assist you and, if necessary, have our developers look into this. You’ll need an active Gravity Perks license to submit a support ticket.

Best,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [August 7, 2023, 7:29pm UTC](https://community.gravityforms.com/t/counting-custom-field-checkboxes-and-input-the-sum-to-product-quantity/15631/5 "2023-08-07T19:29:00Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
