Creating Checkboxes and Text Field Dependency

New Here ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

I have a couple of checkboxes - "Yes" and "No" - and a text field that follows.

 

I'd like to make the text field inactive if the "No" checkbox is checked, but make it active & required if the "Yes" checkbox is selected.

 

Is there a script that can generatre this kind of dependency?

 

(btw, I'm aware that a toggle button would be more funcionally appropriate than 2 independent checkboxes for the "yes" and "no", however I'm working with a fixed PDF template and the checkboxes fit better in that particular context).

 

Thanks!

TOPICS
How to, JavaScript, PDF forms

Views

65

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
Most Valuable Participant ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

What are the names of the fields?

 

And there's no built-in toggle button in PDF forms, so you're good...

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 ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

Edit the name of the text field and use this script as a mouse up action in both radio-buttons:

 

var oFld = this.getField("nameOfTextField");
if (event.target.value == "Yes") {
oFld.readonly = false;
oFld.required = true;
}
else {
oFld.readonly = true;
oFld.required = false;
}

 

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 ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

Thanks, but not sure I follow your reply as I don't have a togle buttons but checkboxes:

 

I have 2 checkboxes (one named "no_checkbox" and the other "yes_checkbox") and I'd like the text field ("details_text") to become active and required if the "yes_checkbox" is ticked.

 

Is it possible to amend the suggested script to accomodate this setup?

 

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 ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

Thanks, I can name the elements any way I like, so say:

"no_checkbox"

"yes_checkbox"

"details_text"

(the idea is to have a yes/no question, and if the answer is "yes" to ask for further details in the text line)

Does this help?

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
Most Valuable Participant ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

It's better to give the check-boxes the same name, so they can't be selected at the same time. Just give them unique export values, like "Yes" and "No". Let's say they are called "Checkbox1". You can then use this tool as the custom calculation script of the text field:

 

var v = (this.getField("Checkbox1").valueAsString=="Yes");
event.target.readonly = !v;

event.target.required = v;

if (!v) event.value = "";

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 ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

Thank you for this!

 

Just to clarify, what do I need to name the text field? (I'm not quite sure I understand how this script for the checkboxes ties in with the text field)

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 ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

Sorry, re-read your answer and realized this is the script for the text field! Sorry!

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 ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

Will try!

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
Most Valuable Participant ,
Mar 04, 2021 Mar 04, 2021

Copy link to clipboard

Copied

The name of the text field doesn't matter.

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