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 15, 2023

Script is not in a dropdown field, it's actually in a text field named "Text3" but you made it small, so it's hard to see.


Many thanks Nesa, good pick up - the last round of changes were made before Christmas so I can't recall how or why the thinking of putting the formula into such a small text box ( a colleague who was familiar with Javascript helped me at the time). Perhaps your earlier reply and the 'hidden text field' was the reason? 

 

I've managed to make the checkboxes mutually exclusive and renamed them 'MultiDisciplinaryPMPCB'. 🙂

Though now the JavaScript I tried to repurpose from that text field doesn't seem to be working as I'd hoped. Not sure if you might be able to see why?

 

For the 'YES' checkbox, I used this 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;

  

}

 

And for the NO value checkbox:

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;

}