Skip to main content
Participant
March 8, 2018
Question

Calculate radio button based on previous radio button selections

  • March 8, 2018
  • 1 reply
  • 495 views

I have a form that will be going through several users; one user will be selecting Y/N for 5 different radio buttons which need to affect the value of a 6th button. My experience with Javascript is limited, so while I have an idea as to how, I'm faltering in the specifics.

If any of the 5 are "No", I need the 6th radio button to automatically change to the "No" selection. I'm planning on an action triggered by mouse-up on each of the "No's" to run a javascript, which would change the default value of the 6th radio button to "No", but I'm not entirely sure how that should be written.

If all of the five are "Yes", the 6th button should automatically change to "Yes". Again, I'm assuming a similar action/javascript as above on each of the "Yes" buttons that would add in some "IF" statements, that way if they're selected in any order the code still works.

Thanks for any help you can give!

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
March 8, 2018

Since the result depends on several other fields, the best approach is to use a calculation script, and since it is somewhat difficult to add a calculation script to a radio button, I'd suggest adding a hidden field to the form that performs the calculation for the Sixth button.

Calculation script on hidden field:

var firstVal = this.getField("FirstButton").value;

if( (firstVal == this.getField("SecondButton")) && (firstVal == this.getField("ThirdButton")) && etc for 4th & 5th buttons)

{

    this.getField("SixthButton").value = firstVal;

}

You'll need to change the field names to the ones on your form. The idea is to first check to see if all the buttons have the same value. If they do then set the 6th button to that value.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
azagorskiAuthor
Participant
March 9, 2018

Took a little jiggery-pokery but this worked!

I added a hidden field to do the calculation, and a hidden Radio button with the default set to "No", and ended up with the following.

var secondVal = this.getField("HiddenRadio").value;

var firstVal = this.getField("FirstButton").value;

if((firstVal == this.getField("SecondButton").value) && (firstVal == this.getField("ThirdButton").value) && (firstVal == this.getField("FourthButton").value) && (firstVal == this.getField("FifthButton").value))

{

this.getField("SixthButton").value = firstVal;

}

else

{

this.getField("SixthButton").value = secondVal;

}

Thanks so much for pointing me in the right direction!

Thom Parker
Community Expert
Community Expert
March 9, 2018

You don't need the hidden radio button.  Just set the Sixth button to "No", instead of secondVal.

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