Skip to main content
Known Participant
August 30, 2020
Answered

Latest code & example

  • August 30, 2020
  • 2 replies
  • 1084 views

Here is the latest code and now it is not working when I click on NO, it owuld reset the fields.  NOw that is broken.

 

 

Everyone,

  I followed this page for  conditional editing:

https://answers.acrobatusers.com/How-I-conditionally-set-required-fields-form-based-user-answers-radio-button-q292291.aspx

 

  Yet this dod not work for me.  I created hidden text field and created the code. It is not working,  then I added to the Mouse Up event on the Radio Button and still is not working.

This what I am trying to do.  Also, what I will need to also figure out is iof the check NO, then I want to initialize the fields to the default value.  I.E. Year = "Select a year (Default value on the List Dropdown)", the other 2 fields are spaces.

If the button is Yes,

    Unprotect the 3 fields and make them required
Else

 IF the button is No, then

   protect the 3 fields and make them not required.

 

Here is my javacript code:

var vYesNo = this.getField("Trade_Yes_No_Grp").value;
var vYearCar = this.getField("Year_Drop_Down");
var vCarMake = this.getField("CMake");
var VCarModel = this.getField("CModel");

 

vYearCar.required = false;
vCarMake.required = false;
vCarModel.required = false;

vYearCar.protected = true;
vCarMake.protected = true;
vCarModel.protected = true;

 

if (vYesNo == "Yes")
{
vYearCar.required = true
vCarMake.required = true
vCarModel.required = true
vYearCar.protected = false
vCarMake.protected = false
vCarModel.protected = false;
}
else if (vYesNo == "No")
{
vYearCar.required = false
vCarMake.required = false
vCarModel.required = false
vYearCar.protected = true
vCarMake.protected = true
vCarModel.protected = true;
}

 

Can someone plz let me know what si wrong with this code or where I should put this java script and the even which it should happen under.  Thanx in advance.

 

Jerry

 

This topic has been closed for replies.
Correct answer ls_rbls

You shouldn't be running that code in both radio button fields.

 

Since you have them set as mutually exclusive all you have to do is split the code.

 

Use this part of your script in the YES Radio Button:

 

//SCRIPT FOR THE YES RADIO BUTTON

this.getField("Year_Drop_Down").required = true;
this.getField("CMake").required = true;
 this.getField("CModel").required = true;
this.getField("Year_Drop_Down").readonly = false;
this.getField("CMake").readonly = false;
this.getField("CModel").readonly = false;

 

 

And use this part in the NO Radio Button:

 

//place this in no radio button
this.getField("Year_Drop_Down").required = false;
this.getField("CMake").required = false;
this.getField("CModel").required = false;
this.getField("Year_Drop_Down").readonly = true;
this.getField("CMake").readonly = true;
this.getField("CModel").readonly = true;

this.resetForm(["Year_Drop_Down", "CMake", "CModel"])

 

That's it.

 

You had the same chunk of script in both radio buttons. If you do it as illustrated in my example you don't need to complicate yourself so much with conditional statements.

2 replies

ls_rbls
Community Expert
ls_rblsCommunity ExpertCorrect answer
Community Expert
August 31, 2020

You shouldn't be running that code in both radio button fields.

 

Since you have them set as mutually exclusive all you have to do is split the code.

 

Use this part of your script in the YES Radio Button:

 

//SCRIPT FOR THE YES RADIO BUTTON

this.getField("Year_Drop_Down").required = true;
this.getField("CMake").required = true;
 this.getField("CModel").required = true;
this.getField("Year_Drop_Down").readonly = false;
this.getField("CMake").readonly = false;
this.getField("CModel").readonly = false;

 

 

And use this part in the NO Radio Button:

 

//place this in no radio button
this.getField("Year_Drop_Down").required = false;
this.getField("CMake").required = false;
this.getField("CModel").required = false;
this.getField("Year_Drop_Down").readonly = true;
this.getField("CMake").readonly = true;
this.getField("CModel").readonly = true;

this.resetForm(["Year_Drop_Down", "CMake", "CModel"])

 

That's it.

 

You had the same chunk of script in both radio buttons. If you do it as illustrated in my example you don't need to complicate yourself so much with conditional statements.

ls_rbls
Community Expert
Community Expert
August 31, 2020

But if you still want to run the mouse-up action code entirely  from the " YES"  Radio Button only, then this works too:

 

if (event.value !=="Off") {

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

this.getField("Year_Drop_Down").required = true;
this.getField("CMake").required = true;
 this.getField("CModel").required = true;
this.getField("Year_Drop_Down").readonly = false;
this.getField("CMake").readonly = false;
this.getField("CModel").readonly = false;

} else {

this.getField("Year_Drop_Down").required = false;
this.getField("CMake").required = false;
this.getField("CModel").required = false;
this.getField("Year_Drop_Down").readonly = true;
this.getField("CMake").readonly = true;
this.getField("CModel").readonly = true;

this.resetForm(["Year_Drop_Down", "CMake", "CModel"])

 }

}

 

 

try67
Community Expert
Community Expert
August 30, 2020

- Place the code as the Mouse Up event of both radio-buttons.

- You can remove the entire first part of it (between the variable declarations and the if-statement).

- There's no property called "protected" in an Acrobat form. Use "readonly", instead.

- To reset the fields if "No" is selected add this command to that section of the code:

this.resetForm(["Year_Drop_Down", "CMake", "CModel"]);

ballj_35Author
Known Participant
August 30, 2020

Try67,

 

  Everything works excluding the protection of the fields when the user clicks "No".  Everything else is great.  It resets the form fine.  Yet, I can go to the fields and enter data.  Trying to make these fields protected so the user cannot get to them.  Here is the code now and any thoughts on what I am doing wrong.  Both radio buttons have the same code in them.

 

if (vYesNo == "Yes")
{
vYearCar.required = true
vCarMake.required = true
vCarModel.required = true
vYearCar.readonly = false
vCarMake.readonly = false
vCarModel.readonly = false;
}
else if (vYesNo == "No")
{
this.resetForm(["Year_Drop_Down", "CMake", "CModel"])
vYearCar.required = false
vCarMake.required = false
vCarModel.required = false
vYearCar.readonly = true
vCarMake.readonly = true
vCarModel.readonly = true;
}

try67
Community Expert
Community Expert
August 30, 2020

The code seems fine. Are there any error messages in the JS Console when you use it? Can you share the file with us?