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

Can the check boxes calculate a percentage number of items completed in PDF?

Community Beginner ,
May 08, 2023 May 08, 2023

Copy link to clipboard

Copied

I have a simple list with checkboxes.

Is there a way to calculate a percentage complete based on the number of checkboxes were checked in one row?

If one checkbox is checked then it gives 25$

If two then 50%

If three then 75%

If all checked then 100%

Screen Shot 2023-05-09 at 9.27.48 AM.JPG

TOPICS
How to , JavaScript , PDF , PDF forms

Views

1.8K

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 Beginner , May 09, 2023 May 09, 2023

SUPPER

It works great, just add the first two lines

 

var max = 4;
var total = 0;
var fields = ["KW MB POP - Dr 1", "KS MB POP - Dr 1", "QT MB POP - Dr 1", "OM MB POP - Dr 1"];
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="Off") {
		total++;
	}
}
event.value = total/fields.length;

Votes

Translate

Translate
Community Expert ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

Sure. Let's say the fields are called Drink1 to Drink4. You can then use this code for that calculation:

 

var max = 4;
var total = 0;
for (var i=1; i<=max; i++) {
	if (this.getField("Drink"+i).valueAsString!="Off") {
		total++;
	}
}
event.value = total/max;

 

 

You'll also need to set the text field as having the Percentage format.

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 ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

Hi try67,

This is supper, Thanks alot.

But, what if the fields name is diffrent like:

KW MB POP - Dr 1

KS MB POP - Dr 1

QT MB POP - Dr 1

OM MB POP - Dr 1

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 ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

Then you need to hard-code them into the script, like this:

 

 

var fields = ["KW MB POP - Dr 1", "KS MB POP - Dr 1", "QT MB POP - Dr 1", "OM MB POP - Dr 1"];
var total = 0;
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="Off") {
		total++;
	}
}
event.value = total/fields.length;

 

[Edited: Code fixed]

 

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 ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

SUPPER

It works great, just add the first two lines

 

var max = 4;
var total = 0;
var fields = ["KW MB POP - Dr 1", "KS MB POP - Dr 1", "QT MB POP - Dr 1", "OM MB POP - Dr 1"];
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="Off") {
		total++;
	}
}
event.value = total/fields.length;

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 ,
May 09, 2023 May 09, 2023

Copy link to clipboard

Copied

Sorry, I accidentally removed the declaration of the total variable.

The max variable is no longer needed, though.

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 ,
Sep 04, 2024 Sep 04, 2024

Copy link to clipboard

Copied

Hello, I would like to ask for assistance on my matter, it's similar to AhmedCOSTA's. 
Calculate the percentage of the checkboxes. 
For example; I have 19 checkboxes in total and if all of them are marked as checked. I want the field to get the percentage of 100%.

I followed Try67's code but I got the result 1,163% despite not ticking any of the boxes. 

Screenshot 2024-09-05 at 6.01.12 AM.pngScreenshot 2024-09-05 at 6.00.37 AM.png

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 ,
Sep 04, 2024 Sep 04, 2024

Copy link to clipboard

Copied

I don't see where you declared 'total' variable?

Other than that it looks ok, although it could be done slightly different.

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 ,
Sep 05, 2024 Sep 05, 2024

Copy link to clipboard

Copied

I see it now. Thank you, Nesa!
I made a mistake in not including the 'total' variable. 

Do you know how to keep that field blank if there's no checkboxes being ticked? 

Thanks again. 

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 ,
Sep 05, 2024 Sep 05, 2024

Copy link to clipboard

Copied

If you format the field as percentage, it can't be blank.

EDIT:

You can use this as custom format script instead:
if(event.value && parseFloat(event.value) != 0)
event.value = (parseFloat(event.value) * 100).toFixed(3) + "%";
else
event.value = "";

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 ,
Sep 05, 2024 Sep 05, 2024

Copy link to clipboard

Copied

It works, Nesa. But it shows with decimals. Is there a way to not have decimals in the percentage?
Sorry, I didn't specify in my previous comment. 
I appreciate all the help. 

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 ,
Sep 05, 2024 Sep 05, 2024

Copy link to clipboard

Copied

LATEST

Disregard my previous comment. 
Here's the script to achieve without decimal.

if
(event.value && parseFloat(event.value) != 0)

event.value = (parseFloat(event.value) * 100).toFixed(0) + "%";
else
event.value = "";

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