• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to calculate the number of checked checkboxes??

New Here ,
Jan 18, 2017 Jan 18, 2017

Copy link to clipboard

Copied

I have 90 check boxes in a pdf form. I want to calculate the total number of check boxes being checked in the form and show the result automatically in the "total" field. How can I do that? I do not know about scripts. Please help. Thank you!

TOPICS
PDF forms

Views

9.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 19, 2017 Jan 19, 2017

Use this code as the custom calculation script of the "total" field:

var total = 0;

for (var i=1; i<=90; i++) {

    if (this.getField("cb"+i).value!="Off") total++;
}

event.value = total;

Votes

Translate

Translate
Community Expert ,
Jan 19, 2017 Jan 19, 2017

Copy link to clipboard

Copied

Use this code as the custom calculation script of the "total" field:

var total = 0;

for (var i=1; i<=90; i++) {

    if (this.getField("cb"+i).value!="Off") total++;
}

event.value = total;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 19, 2017 Jan 19, 2017

Copy link to clipboard

Copied

Sorry try67! I'm very stupid at JS. I've input the script and no result shown. Did I make it wrong?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2017 Jan 20, 2017

Copy link to clipboard

Copied

Did you enter it under the field's action, as I've described, or did you use the Edit All JavaScripts command? If the latter, you may have corrupted the file...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 20, 2021 Mar 20, 2021

Copy link to clipboard

Copied

Thank you - this is working perfectly. My problem is that I'm working in pages in pdf. It works wonderfly for page 1, but when it comes to page 2, I've followed the same 'formula' and renamed my check boxes but it simply won't calculate my boxes. Should I be adding in a page number reference somewhere in the above javascript somewhere to indicate its on a different page, etc.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2021 Mar 20, 2021

Copy link to clipboard

Copied

No, the page it's on doesn't matter. Check the JS Console for error messages. If you can't find a solution share the file and we'll help you out with it further.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

This is my first time to ever use scripts but I follow directions pretty well. I have a PDF form with 146 checkboxes simply labeled 1, 2, 3, ... 146. I found this script on this community page that is doing the same thing I want to do but for only 90 checkboxes. The only thing I changed in the script below is to change the 90 to 146. It doesn't work so can you please tell me what I am doing wrong? Thank you!

var total = 0;
for (var i=1; i<=146; i++) {
if (this.getField("cb"+i).value!="Off") total++;
}
event.value = total;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

In that code you are using field name "cb" and you said your checkboxes are labeled just 1,2,3...etc so change  "cb" to "" it should look like this:

var total = 0;
for (var i=1; i<=146; i++) {
if (this.getField(""+i).value!="Off") total++;
}
event.value = total;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2022 Apr 03, 2022

Copy link to clipboard

Copied

I had exactly the same problem as the original poster. Your code is perfect. Thank you very much!

What I struggled with:

I had 3 check boxes numbered e.g. 19, 20, 21.

The loop started at 1 and ran to 150.

This did not work.

Also when my check boxes were numbered 1, 2, 3, this did not work.

I had to start the loop with the correct number and restrict it to the correct total.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

I've entered it under the field's action. But when I debug the script, it shows the following error.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

You are not using the correct field name to get the field objects. JavaScript is case sensative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 22, 2017 Jan 22, 2017

Copy link to clipboard

Copied

Thank you try67!! I know the problem now..... As I have revised the file many times and it is corrupted as you said. I used the original file and enter the script again and it works!!

Thank you very much!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Hi all, is there a way to have the total box be blank rather than show a 0   when nothing is selected?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 07, 2022 Oct 07, 2022

Copy link to clipboard

Copied

LATEST

Just in case someone stumbles upon this discussion looking for the answer, I found another way to achieve the same goal.  

 

Select ALL the checkboxes > Double click and select OPTIONS > change EXPORT VALUE to 1

Select the TOTAL field > Double click and select VALUE IS THE choose SUM > click PICK and now select all the checkboxes

Boom.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines