Javascript for conditionally requiring radio buttons to be checked

New Here ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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;

}

TOPICS
Acrobat SDK and JavaScript

Views

208

Likes

translate

Translate

Translate

Report

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

correct answers 1 Correct Answer

Adobe Community Professional , Dec 11, 2017 Dec 11, 2017
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 butt...

Likes

translate

Translate

Translate
Adobe Community Professional ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 11, 2017 Dec 11, 2017

Copy link to clipboard

Copied

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

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

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 13, 2017 Dec 13, 2017

Copy link to clipboard

Copied

Thank you so much, Thom!  That works great!  However, how do I write JavaScript to clear out the answer if the user changes their answer from yes to no and has already checked the box in Subgroup1c?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 13, 2017 Dec 13, 2017

Copy link to clipboard

Copied

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

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

this.getField("Subgroup1c").value = "Off";

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

Likes

translate

Translate

Translate

Report

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