Skip to main content
December 11, 2017
Answered

Javascript for conditionally requiring radio buttons to be checked

  • December 11, 2017
  • 1 reply
  • 1942 views

I am creating a PDF form that consists of several yes and no questions (with radio buttons) and I am trying to conditionally require subquestions (if yes is checked).  My question is Group1c, my subquestion is Subgroup1c.  My answer choices for Group1c are Yes and No.  All are radio buttons.  My first question is already marked required, and my subquestion should be conditionally required.  I tried the following code, but it keeps returning an error.  Please advise!  Thank you!

var f = this.getField("Group1c");

var yes = this.getField("Yes");

yes.required = false;

if(f.value === "Yes") {

Subgroup1c.required = true;

}

This topic has been closed for replies.
Correct answer Thom Parker

Thank you.  My individual radio buttons are named Yes and No.  I wanted it to be that if yes was hit, then Subgroup1c is required, but if no was hit, then Subgroup1c is not required. I'll add in a line defining Subgroup1c (var sub = this.getField("Subgroup1c");)  but how do I require the Subgroup1c correctly?


Maybe I wasn't clear. Radio buttons act as a group.  They act as a group because they all have the same field name. Therefore you cannot give buttons in the same group different names, because then they'd be in different groups. So, in fact you do not have any radio buttons named Yes and No. 

***Put this code in the MouseUp Action of the "Group1c" radio button that exports a "Yes" value.

this.getField("Subgroup1c").required = true;

***Put this code in the MouseUp Action of the "Group1c" radio button that exports a "No" value.

this.getField("Subgroup1c").required = false;

You do not need to use the "if" statments to check values on the button because when the user presses Yes radio button and activates the script on that button, we already know it is for Yes

1 reply

Thom Parker
Community Expert
Community Expert
December 11, 2017

Just some general questions first. What is the error you speak of?  Did you see it in the Console Window? If not, where did you see the error message? Where is this script used?  

Next, the "Subgropu1c" parameter is not declared or set anywhere.  Is this your error?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
December 11, 2017

When I hit the yes radio button, the Console Window/ JavaScript Debugger pops up saying:

7:AcroForm:Group1c:Annot1:MouseUp:Action1Yes

TypeError: yes is null

I put the script in a mouse up/JavaScript action under the radio button for Group1c.

My Subgroup1c is already labeled/defined as that within my form; do I need to add code to define it further, as part of making it conditionally required?  Thank you.

Thom Parker
Community Expert
Community Expert
December 11, 2017

It is reporting that "yes" is null because - getField("Yes")  - is returning null, meaning that there isn't a field named "Yes" on the form.

So you were under the impression that one of the radio buttons was named "Yes"?

"Yes" is probably one of the export values?  And you were trying to get the individual button?  This is not how you acquire an individual button in a group, and you can't set a single radio button in a group to "required". The group as a whole is either required or not.

It doesn't matter where "Subgroup1c" is defined outside this script. Inside the script it is a variable that has not been declared, nor does it have a value.

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