Counting custom field checkboxes and input the sum to product quantity

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.

Hi Compute,

I am unsure about your Javascript snippet, but looking at what you’re trying to do, I think our Choice Counter snippet will work for you. Here’s the documentation on how to use the snippet and how it works.

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.

Hi,

Trial two should work. However, if it’s still not working for you, you’ll need to contact us via our support form 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,

1 Like

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