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

Question

Question

Calculate Number of Checkboxes that are checked

asked on December 14, 2016

We are trying to put a calculation that counts the number of check boxes that are checked and returns the value. Can anyone provide guidance on how to accomplish this?  For example we would have the following multi-select choices:

Choice A, Choice B, Choice C, Choice D, etc. 

There would be a new box that would return the total of those boxes.  

Thanks!

0 0

Answer

SELECTED ANSWER
replied on December 21, 2016

Here is an example that puts it all together.

Below shows the layout of the form as well as the javascript being used. Note that each checkbox field uses the CSS class name, "checkbox" while the checkbox sum and fee fields use "checkboxsum" and "fee" respectively.

In the "Layout" view, make sure that none of the fields are marked as read only. The javascript above handles that already. In the fee field, it also uses the formula

=IF(Checkbox_Sum<1,0,IF(Checkbox_Sum<20,Checkbox_Sum,IF(Checkbox_Sum<60,PRODUCT(Checkbox_Sum,2),PRODUCT(Checkbox_Sum,3))))

The variable name of the checkbox sum field is "Checkbox_Sum" in this example.

This video will show a user starting the business process, submitting the form, and an approver approving it.

0 0

Replies

replied on December 14, 2016

Is the calculation done after the submit or in real time on the form?  If it is after submit you would probably want to run a workflow.  Run a parrallel that checks to see if each box is checked.  If it is, update a token value, if not, dont update it.  Run a Token calculator on it and then pass that forward to your next form using Set Business Processes.  If it is in real time on the same for you would probably need to write some kind of custom CS.

1 0
replied on December 14, 2016

Hi - we would want to do it in real time.  I was thinking I could set the value of each choice to 1 and then have a way to add the values of all selected choices.  But I am really not sure.

 

0 0
replied on December 14, 2016

Does the JavaScript example from this page regarding "Adding checkbox field values and populating a field with their sum" suffice? This goes with what you stated where you give each checkbox option a value of 1.

0 0
replied on December 14, 2016

That is definitely what we need, only I need the field to populate the value from multiple choices.  So if three checkboxes are checked the value would return as 3.  

There is a notation that this can be done with some modification to the javascript, any thoughts on what changes would be necessary?

Thanks!

0 0
replied on December 14, 2016

Actually, you don't need to set a value for each checkbox choice you have. You just need to slightly modify the JS example from the help page.

On your form, give each checkbox field you have a CSS class name "checkbox" while also having a single line field with "checkboxsum" as the CSS class name.

Then you can use this JS

$(function() {
    $(".checkbox").change(function () {
        var sum = 0
        $(".checkbox input:checked").each(function(){
            sum += 1;
        });
        $(".checkboxsum input").val(sum);
    });
});

0 0
replied on December 15, 2016

This worked beautifully - now I have a follow-up question related to this and another topic you helped me on:

https://answers.laserfiche.com/questions/112262/Calculation-Forms-10#repliesToMain

I am using the total number of checkboxes checked to trigger a calculation, but combining these two together doesn't work.  The formula above accurately totals the number of check boxes, but does not trigger the calculation in the referenced link.  If I manually type a number in the "Sum" box then the calculation field displays the total amount due.

Thanks again!

0 0
replied on December 15, 2016

You'll need to trigger a "change" event on the field that represents the checkboxsum for it to have the formula use the number from there to calculate the fee. You can use

$(function() {
    $(".checkbox").change(function () {
        var sum = 0
        $(".checkbox input:checked").each(function(){
            sum += 1;
        });
        $(".checkboxsum input").val(sum);
        $(".checkboxsum input").trigger('change');
    });
});
0 0
replied on December 15, 2016

Worked perfectly!  Thank you!

0 0
replied on December 20, 2016

Update on this:  The caculated value is not coming through when the task is assigned. It works prior to submission, but not after.  So the approval task shows $0.  

Any thoughts on this?

Thank you.

No Money.PNG
No Money.PNG (23.93 KB)
0 0
replied on December 20, 2016

It appears this has to do with the field being "Read Only".  When I remove this option it works upon submission.  

0 0
replied on December 20, 2016

Leave the "read only" option for that field unchecked on the form itself, but use javascript to make the field read only. See if that addresses the matter.

0 0
replied on December 21, 2016 Show version history

Makes sense - so if I use this:

$('.campus input').prop('readonly',true);

Where do i get the value that goes where it says .campus input in the above example?

I tried  .checkboxsum but that doesn't seem to work.

Thanks!

0 0
SELECTED ANSWER
replied on December 21, 2016

Here is an example that puts it all together.

Below shows the layout of the form as well as the javascript being used. Note that each checkbox field uses the CSS class name, "checkbox" while the checkbox sum and fee fields use "checkboxsum" and "fee" respectively.

In the "Layout" view, make sure that none of the fields are marked as read only. The javascript above handles that already. In the fee field, it also uses the formula

=IF(Checkbox_Sum<1,0,IF(Checkbox_Sum<20,Checkbox_Sum,IF(Checkbox_Sum<60,PRODUCT(Checkbox_Sum,2),PRODUCT(Checkbox_Sum,3))))

The variable name of the checkbox sum field is "Checkbox_Sum" in this example.

This video will show a user starting the business process, submitting the form, and an approver approving it.

0 0
replied on December 21, 2016

I really appreciate the guidance.  All is working as expected.  Thank you!

0 0
replied on October 11, 2017

Alex is there a way to total the number of Choices? For example, A number field labeled Choice A Total : (calculates the total Choice A that was checked and another number field for total checked Choice B?

0 0
replied on October 13, 2017
0 0
replied on April 20, 2021

See the following Post for information on how to get the total number of check boxes selected, in the event this may be another solution for you.

https://answers.laserfiche.com/questions/171042/Using-a-formula-to-count-checkboxes#186312

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

Sign in to reply to this post.