You are viewing limited content. For full access, please sign in.

Question

Question

Sum of Checkbox Values

asked on June 15, 2023 Show version history

We have a form with multiple products grouped into categories.  Each group of products is in a checkbox field with values that combine the product ID and the cost.  We would like to get a sum of the costs for the selecteditems in each group. 

We are already doing something similar on another form that uses a table instead of checkboxes.  In that form, there is the Sets table with a dropdown field called AddressSets.  The dropdown field has a list of choices and values:

The first three characters of the value is the Product ID, the second two are the postage and the last ones are the cost.  The formula for the Price field in the row is =MID(INDEX(Sets.AddressSets,ROW()),6,100).  The formula for Postage is =IF(SendTo="PickUp",0,IF(LabelsLists="Lists",0,MID(INDEX(Sets.AddressSets,ROW()),4,2))).  The Total Cost formula is SUM(Sets.Total).  

Can we do something similar with a checkbox field?  Is there something like =MID(INDEX(Sets.AddressSets,ROW()),6,100) for getting the values for all the selected items?

0 0

Replies

replied on June 16, 2023 Show version history

Based on your screenshot, I can see you are using the Classic Designer.  I'm testing here with the Classic Designer on version 11.0.2212.30987.

The trick here is how the calculated formulas handle checkboxes.  In you go into a calculation on a field and use the variable picker, you'll see that it does not provide an option to pick the variable for the entire checkbox field, but just for specific values in the checkboxes.

Example:     =my_checkbox.FUL10225

And this formula just returns TRUE or FALSE based on whether the particular checkbox is marked or not.  It doesn't return a list of the checked values for the field.

So I'm not certain we're going to make this work in a meaningful way with the formulas.

Additionally - your values of the 1st, 2nd, 3rd, etc. will all be automatically changed to include a V_ before them because checkbox values in Forms cannot start with a number.  So you might need to decide on a different way to do those values so that they are not starting with a number.  For my testing, I just used D01, D02, D03, etc. (to mean District 1, District 2, District 3, etc.)

But, the good news is that we can always do a script to achieve your needs for this total of the checkboxes.  Here's a Javascript that should work.  This assumes your checkbox field has a class name of myCheckboxes, and the total field has a class name of myCalculation. 

$(document).ready(function() {
  
  //Run the calculation function when the form is first loaded.
  CalculateMyCheckboxes();
  
  //When changes happen on the myCheckboxes field, run the calculation function.
  $('.myCheckboxes input').change(CalculateMyCheckboxes);
  
  //Calculation function for the checkboxes.
  function CalculateMyCheckboxes() {
    var costTotal = 0;
    $('.myCheckboxes input:checked').each(function() {
      var checkboxCost = parseFloat($(this).val().substring(5, 99));
      costTotal = costTotal + checkboxCost;
    });
    $('.myCalculation input').val(costTotal);
  }
  
});

 

                        225 + 30 + 75 = 330

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.