• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

grouped radio buttons switching required properties on and off

Explorer ,
Aug 17, 2018 Aug 17, 2018

Copy link to clipboard

Copied

Hello me again

I've got some grouped radio buttons on a form 2 sets 1 set is a yes or no answer and the 2nd set should only be completed if the answer t

Cash Yes No

Cash Controlled Yes No

If the answer to cash is Yes the user must complete CashControlled, if the answer is No they can move on to the next question.

I've got following code in Cash Yes field on mouse up

if (event.target.value == "Yes") {

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

    }

and in the Cash No field I have

if (event.target.value == "No") {

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

    }

This works but if a user goes back and selects a different option the fields don't update correctly for example

if the user selects No on Cash and then changes to yes the field doesn't update to required and vice versa

What am I doing wrong?

TIA

Ellen

TOPICS
PDF forms

Views

1.0K

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

LEGEND , Aug 20, 2018 Aug 20, 2018

It is possible to create a document level JavaScript function that would work for both radio buttons and check boxes that performs this task. Each button or check box would have the following "Mouse Up" action:

SetRequiredbyRB("CashControlled");

And then add the following document level function:

function SetRequiredbyRB(cRequiredField) {

var oRequiredField = this.getField(cRequiredField);

var bRequired = false;

function MakeReadOnly() {

bRequired = "Off"; // remove required;

    oRequiredField.readonly

...

Votes

Translate

Translate
Community Expert ,
Aug 17, 2018 Aug 17, 2018

Copy link to clipboard

Copied

two equals sets the value three ask for an exact match.

you need to have === not ==

Personally I prefer to separate definitions and the if statement so that I can reuse it.

I would use this script on both radio buttons on Mouse Release:

var Q = this.getField("Cash");

var A = this.getField("CashControlled");

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

    A.required = true;    

}

if (Q.value === "No") {

    A.required = false;    

}

Votes

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
LEGEND ,
Aug 17, 2018 Aug 17, 2018

Copy link to clipboard

Copied

Unless one of the radio buttons is set as being checked by default, there exist the situation where nether button has been selected and the value of the group is neither "Yes" nor "No".  I would also clear the selection of the "CashControlled" radio button group. When there is no button selected within a radio button group, the value of the group is "Off". The "Cash" field will not have a selection when created or the form is reset unless one of the radio buttons in the group is set to be checked by default;

if (event.target.value == "Yes") {

     // Yes is selected;

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

    } else {

    // any other answer than "Yes" included "No" and "Off" (no selected button);

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

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

    }

If both buttons run the same code, then the possibility of

Votes

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
Explorer ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

Hello

Thanks for your help

Does the code get added to the Cash Yes No radio buttons?

TIA

Ellen

Votes

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
Community Expert ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

Yes.

Votes

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
Community Expert ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

https://forums.adobe.com/people/Lukas+Engqvist  wrote

two equals sets the value three ask for an exact match.

you need to have === not ==

...

This is not correct. One equal sets the value.

Votes

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
Community Expert ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

yes sorry

you are right

Votes

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
LEGEND ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

LATEST

It is possible to create a document level JavaScript function that would work for both radio buttons and check boxes that performs this task. Each button or check box would have the following "Mouse Up" action:

SetRequiredbyRB("CashControlled");

And then add the following document level function:

function SetRequiredbyRB(cRequiredField) {

var oRequiredField = this.getField(cRequiredField);

var bRequired = false;

function MakeReadOnly() {

bRequired = "Off"; // remove required;

    oRequiredField.readonly = true; // make read only;

    oRequiredField.value = ""; // clear value;

    return;

}

switch(event.target.value) {

case "Yes":

bRequired = true; // make required;

oRequiredField.readonly = false; // make writable;

oRequiredField.setFocus(); // jump to field;

break;

case "No":

case "Off":

    MakeReadOnly();

    break;

    default:

    MakeReadOnly();

    // issue warning;

    app.alert("Unkown response " + event.target.value + " for " + event.target.value, 1, 0, "Unkonw Response");

    break;

} // end switch event.target.value;

oRequiredField.required = bRequired; // set required property;

    return bRequired; // return reuired value;

} // end SetREquiredbyRB function;

The above code does not rely on the on using the "==" or "===" comparison operator and can be used with any number of fields that require this type of action by just changing the field name parameter in the called function for the radio button or check box. It also reports if group of buttons has a new selection added and the function is not update for this change.

Votes

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