Skip to main content
Mach680
Participating Frequently
February 9, 2023
Question

Can I do a custom calculation script for a radio button selection (or checkboxes)?

  • February 9, 2023
  • 2 replies
  • 3471 views

Hi all,

 

First time poster, long time lurker. 🙂

 

Am a bit of an Adobe newbie - especially on JavaScript so am hoping someone can kindly assist/advise?

 

I am helping develop a form with an existing dropdown (yes/no) question where depending on if 'yes' or 'no' is selected, subsequent questions are presented to the user based on their selection.

 

I've been asked if it's possible to do this with check boxes (instead of a dropdown). Given the question is mutually exclusive (must be either 'yes' or 'no'), I thought radio buttons might make more sense here - however, when I go into the radio button field properties, I noted there is no 'calculate' tab (as that might appear when using the dropdown field for instance). And with no calculate tab, I am unable to identify if/where any javascript calculation would go (if at all possible?).

 

So I guess my question is if it's possible to convert the existing javascript (used in the dropdown) to either radio buttons or check boxes (the subsequent form follow up questions remain the same, it's just an 'aesthetic' change requested by the team who use/manage the form.

 

Hopefully the above makes sense. Happy to clarify or provide further details/screens if required.

 

Cheers!

 

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
February 9, 2023

Do you want the value of the radio button to changed based on a calculation script?  Or are you using the calculation script to set some other value based on the radio button?  

 

You can read more about calculations here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Nesa Nurani
Community Expert
Community Expert
February 9, 2023

In checkbox/radio buttons you need to use 'Action' tab for example as 'Mouse UP' and then select 'Run a JavaScript' and put script there, but unlike calculate tab it only runs script when you click on that field (which can be good in some cases) because calculate script runs every time you make change in your file, and you may not need script to run all the time.

You can also make checkboxes mutually exclusive by giving both checkboxes same name but give one checkbox export value of 'Yes' and other 'No', which can be better than radio button because you can uncheck them, and you can't do that with radio buttons. You will probably need script in both checkboxes to show/hide fields, or you can even put script in hidden text field as calculate script if needed. If you post your script or your file we can be of better assistance.

Mach680
Mach680Author
Participating Frequently
February 14, 2023

Hi Nesa, many thanks for your reply and sorry for the delay getting back to you - have been offline from work for a few days since I posted my original query.

Thanks for the suggestion on the checkboxes, that may be the better option here from a user POV.

 

Have attached the PDF page and some screenshots of the 'yes' and 'no' state when selected in the dropdown list - these javascripts would ideally then need to be converted to be used on the 2 new checkboxes below the dropdown.

 

Bizarrely, when I click on the dropdown (the javascript works) - however, when I visit the calculate tab, the javascript is not show in the custom calculation section (but is still recorded in the javascript editor) - have come across this a few times on my various forms so I'm not sure if that may suggest an issue with my Adobe Acrobat DC perhaps...Hopefully you will be able to see the Javascript calculation on your end.

 

In any case, this was the javascript used for the dropdown I believe

 

var e = this.getField("MultidisciplinaryPMProgram");
if ((e.value == 1) ) {
this.getField("Header2").display = display.hidden;
this.getField("Header2Ans").display = display.hidden;

this.getField("Button1").display = display.hidden;
this.getField("TypeOfProgram1").display = display.visible;
this.getField("TypeOfProgram2").display = display.visible;
this.getField("TypeOfProgram3").display = display.visible;
this.getField("DateOfProgCompletion1").display = display.visible;
this.getField("DateOfProgCompletion2").display = display.visible;
this.getField("DateOfProgCompletion3").display = display.visible;
this.getField("MDPMWorkersResponseRow1").display = display.visible;
this.getField("MDPMWorkersResponseRow2").display = display.visible;
this.getField("MDPMWorkersResponseRow3").display = display.visible;
}
else {
this.getField("Header2").display = display.visible;
this.getField("Header2Ans").display = display.visible;

this.getField("Button1").display = display.visible;
this.getField("TypeOfProgram1").display = display.hidden;
this.getField("TypeOfProgram2").display = display.hidden;
this.getField("TypeOfProgram3").display = display.hidden;
this.getField("DateOfProgCompletion1").display = display.hidden;
this.getField("DateOfProgCompletion2").display = display.hidden;
this.getField("DateOfProgCompletion3").display = display.hidden;
this.getField("MDPMWorkersResponseRow1").display = display.hidden;
this.getField("MDPMWorkersResponseRow2").display = display.hidden;
this.getField("MDPMWorkersResponseRow3").display = display.hidden;

}

 

 

 

Mach680
Mach680Author
Participating Frequently
February 22, 2023

In "NO" you used "MultidisciplinaryPMPCB" 'd' should be 'D'.

Also, in both checkboxes you use: e.value == 1 if they are mutually exclusive they can't have the same export value.

'else' part will work only when you uncheck checkbox manually, it won't work if let's say you select "YES" and then you select "NO".


Hi Nesa,

 

Thanks again and sorry for the delayed response on my part. Juggling a few pieces of work on the go 🙂

 

I have fixed the 'NO' checkbox with the fixed typo & also changing the e.value '==1' to '==0'

 

Unfortunately, I'm still having troubles trying to get the script to work (currently, nothing happens with either YES or NO boxes checked). Would this be due to the 'else' part perhaps? Any further assistance would be much appreciated as I'm not sure where else to turn for help 🙂

 

YES checkbox has the following script

var e = this.getField("MultidisciplinaryPMPCB");
if ((e.value == 1) ) {
this.getField("Header2").display = display.hidden;
this.getField("Header2Ans").display = display.hidden;

this.getField("Button1").display = display.hidden;
this.getField("TypeOfProgram1").display = display.visible;
this.getField("TypeOfProgram2").display = display.visible;
this.getField("TypeOfProgram3").display = display.visible;
this.getField("DateOfProgCompletion1").display = display.visible;
this.getField("DateOfProgCompletion2").display = display.visible;
this.getField("DateOfProgCompletion3").display = display.visible;
this.getField("MDPMWorkersResponseRow1").display = display.visible;
this.getField("MDPMWorkersResponseRow2").display = display.visible;
this.getField("MDPMWorkersResponseRow3").display = display.visible;
}
else {
this.getField("Header2").display = display.visible;
this.getField("Header2Ans").display = display.visible;

this.getField("Button1").display = display.visible;
this.getField("TypeOfProgram1").display = display.hidden;
this.getField("TypeOfProgram2").display = display.hidden;
this.getField("TypeOfProgram3").display = display.hidden;
this.getField("DateOfProgCompletion1").display = display.hidden;
this.getField("DateOfProgCompletion2").display = display.hidden;
this.getField("DateOfProgCompletion3").display = display.hidden;
this.getField("MDPMWorkersResponseRow1").display = display.hidden;
this.getField("MDPMWorkersResponseRow2").display = display.hidden;
this.getField("MDPMWorkersResponseRow3").display = display.hidden;

}

 

 

NO checkbox now has the following javascript:

 

var e = this.getField("MultidisciplinaryPMPCB");
if ((e.value == 0) ) {
this.getField("Header2").display = display.hidden;
this.getField("Header2Ans").display = display.hidden;

this.getField("Button1").display = display.hidden;
this.getField("TypeOfProgram1").display = display.visible;
this.getField("TypeOfProgram2").display = display.visible;
this.getField("TypeOfProgram3").display = display.visible;
this.getField("DateOfProgCompletion1").display = display.visible;
this.getField("DateOfProgCompletion2").display = display.visible;
this.getField("DateOfProgCompletion3").display = display.visible;
this.getField("MDPMWorkersResponseRow1").display = display.visible;
this.getField("MDPMWorkersResponseRow2").display = display.visible;
this.getField("MDPMWorkersResponseRow3").display = display.visible;
}
else {
this.getField("Header2").display = display.visible;
this.getField("Header2Ans").display = display.visible;

this.getField("Button1").display = display.visible;
this.getField("TypeOfProgram1").display = display.hidden;
this.getField("TypeOfProgram2").display = display.hidden;
this.getField("TypeOfProgram3").display = display.hidden;
this.getField("DateOfProgCompletion1").display = display.hidden;
this.getField("DateOfProgCompletion2").display = display.hidden;
this.getField("DateOfProgCompletion3").display = display.hidden;
this.getField("MDPMWorkersResponseRow1").display = display.hidden;
this.getField("MDPMWorkersResponseRow2").display = display.hidden;
this.getField("MDPMWorkersResponseRow3").display = display.hidden;

}