# Adding values of checkboxes with acrobat forms and dividing it by 2

Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

So, I am trying to develop a risk assessment calculator. I have almost completed the project, however there is this weird issue I am struggling to deal with. The user is presented a list of circumstances and, if they apply to the experiment they are doing, they check the box. Each box is worth one point and the total at the bottom should sum these up and then divide that sum by 2 and present the value rounded up. I am able to get the sum by just using the simple calculation feature. However, to do the divided by 2 and the rounding up, I have to use javascript calculations. Unfortunately, this doesn't seem to be working and I am not quite sure how to approach this. Any suggestions would be appreciated, thank you.

TOPICS
How to, JavaScript, PDF forms

Views

379

Likes

Report

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

Adobe Community Professional , Mar 15, 2021 Mar 15, 2021
Your check box name is "probRiskFactor2a"  change a to A.

Likes

20 Replies 20
Mar 15, 2021 Mar 15, 2021

Copied

Whats name of checkboxes and how many you have?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

There are thirteen checkboxes labelled probRiskFactor1A, probRiskFactor2A, probRiskFactor3A, all the way to probRiskFactor13A.

Likes

Report

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

Copied

Remove "A" from field names and use this code :

``````var total = 0;
for ( var i=1; i<=13; i++){
if(this.getField("probRiskFactor"+i).valueAsString != "Off")total++;
if(total == 0) event.value = "";
else event.value = Math.ceil(total/2);}``````

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

Thank you. Is there anything preventing me from changing

``"probRiskFactor"+i``

to "probRiskFactor"+i+"A". The reason I have the A is because this thing repeats itself for four different hazard classes. If not I can just keep increasing the numbers but if I could keep the naming system that would be cool. Thank you for the help though, I will try this out.

Likes

Report

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

Copied

Sorry about that, yes you can use it like that.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

Hm, the script doesn't seem to be working as no value is being output into the textbox.

Likes

Report

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

Copied

Where did you put script?

Paste script you currently using here.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

var total = 0;
for ( var i=1; i<=13; i++){
if(this.getField("probRiskFactor"+i+"A").valueAsString != "Off")total++;
if(total == 0) event.value = "";
else event.value = Math.ceil(total/2);}
Here is the script I am using. I put this in "Custom Calculation Script" box under the properties of the textbox. My apologies if I am making some basic error, this is my first time using acrobat to generate a form and my first time with javascript.

Likes

Report

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

Copied

The final 'if' block that sets the value needs to be outside the loop, because the field value is only set once at the end of the calculation

``````var total = 0;
for ( var i=1; i<=13; i++)
{
if(this.getField("probRiskFactor"+i+"A").valueAsString != "Off")
total++;
}
if(total == 0)
event.value = "";
else
event.value = Math.ceil(total/2);``````

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

I made that change and a value still isn't being output.

Likes

Report

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

Copied

That should work. can you share your file?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

I am afraid I dont know what you mean share the file. I did not write it as an external javascript file and call it in acrobat. I directly pasted the script into the calculation box.

Likes

Report

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

Copied

Also try Thom advice and see if that helps.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

Here is the link. I have only done this for the probability risk factors under "chemical hazards". Thank you for taking a look.

Likes

Report

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

Copied

Share the PDF file.

However, the first thing you need to do is look in the console window to see if there are any errors reported. It is highly likely that the field names in the code are incorrect.

Press Control-J to display the console window.

You'll find a tutorial on it here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

I think there may be an error in the way I am integrating the script as the debug menu shows nothing. I will take a look through the tutorials you have shared to see if I can figure that out. Thank you for the tip.

Likes

Report

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

Copied

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

Likes

Report

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

Copied

Your check box name is "probRiskFactor2a"  change a to A.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Mar 15, 2021 Mar 15, 2021

Copied

LATEST

Wow, I actually noticed that error earlier, changed it, but must have forgotten to save the change. It is working now, thank you so much for the help.

Likes

Report

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