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

How can I hide or make visible checkboxes based on a single checkbox selection?

New Here ,
Nov 10, 2017 Nov 10, 2017

Copy link to clipboard

Copied

I am creating a form to ask:

Have you had any of the following in the past 12 months? (Check all that apply or check here if None [ ]<--named "None Apply" )

checkbox.1  Problem maintaining balance.

checkbox.2  Mental health problems.

checkbox.3  Shortness of breath

checkbox.4  Chemical Dependency

checkbox.5  Needed emergency care

checkbox.6  Other (Explain: [TextboxOther])

My checkbox JS is as follows:

#1 var oAll = this.getField("checkbox");

#2 var aAll = oAll.getArray();

#3

#4 //if "None Apply" checkbox is not checked[Off] then checkbox.1-6 remain visible. If checked then hide checkbox.[1-6] if unchecked become visible again.

#5 if (event.target.value !== "Off") {

#6 //set the check boxes to hidden if none apply;

#7 for(var i = 0; i < aAll.length; i++){

# 8 this.getField("aAll.").display = display.hidden;

#9    console.println(aAll..value);

#10 }

#11 }

#12 else {

#13 //set the check boxes to visible if the none box not checked;

#14 for(var i = 0; i < aAll.length; i++){

#15 this.getField("aAll.").display = display.visible;

#16 }

# 17}

I am getting a syntax error in line 11 that I cannot see for the life of me.  Slowly going bald...

TOPICS
Acrobat SDK and JavaScript , Windows

Views

4.2K

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

Community Expert , Nov 13, 2017 Nov 13, 2017

There are a number of issues with your code, get rid of it all.

I'm assuming that the script is for a "MouseUp" action on the "None" checkbox?

If so, then this script will work

this.getField("checkbox").display = (event.target.value=="Off")?display.visible:display.hidden;

Votes

Translate

Translate
Community Expert ,
Nov 13, 2017 Nov 13, 2017

Copy link to clipboard

Copied

There are a number of issues with your code, get rid of it all.

I'm assuming that the script is for a "MouseUp" action on the "None" checkbox?

If so, then this script will work

this.getField("checkbox").display = (event.target.value=="Off")?display.visible:display.hidden;

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

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
New Here ,
Nov 14, 2017 Nov 14, 2017

Copy link to clipboard

Copied

Hey Thom,

First off, Whoa! I have been ghosting the forums a while and I never thought I would have you answer my stupid newbie question! I always wonder at how you can do the same task in on or 2 lines of code that it takes me half a page to do. Thanks for your attention!!!

Yes, the script is for a "MouseUp" action on the "None" checkbox.

I ended up using a simple If then else that works:

//choose check box and hide checkboxes beneath this one

if ((this.getField("None Apply").value) !== "Off") {

//set the check boxes to hidden if none apply;

this.getField("Check Box1").display = display.hidden;

this.getField("Check Box2").display = display.hidden;

this.getField("Check Box3").display = display.hidden;

this.getField("Check Box4").display = display.hidden;

this.getField("Check Box5").display = display.hidden;

this.getField("Check Box6").display = display.hidden;

this.getField("Other Issues").display = display.hidden;

}

else {

//set the check boxes to visible if the none box not checked;

this.getField("Check Box1").display = display.visible;

this.getField("Check Box2").display = display.visible;

this.getField("Check Box3").display = display.visible;

this.getField("Check Box4").display = display.visible;

this.getField("Check Box5").display = display.visible;

this.getField("Check Box6").display = display.visible;

}

How does the code you suggested work?

this.getField("checkbox").display = (event.target.value=="Off")?display.visible:display.hidden;

|-----------I get this part ok--------|    |-is this shorthand for if ("None Apply" == "Off") then display.visible else display.hidden--|

How can I do the this with the variable names I have listed here?

What naming convention should I normally use for multiple fields or checkboxes?

Where in the Adobe Acrobat DC SDK should I be focusing my attention?

I don't want you to write my code for me, I want to figure out how to create a workable solution so I can be an effective coder myself.

Again, I am a big fan and I appreciate your input and suggestions.

Thank you,

Dean

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 ,
Nov 14, 2017 Nov 14, 2017

Copy link to clipboard

Copied

In the original post the field names use the "dot" notation, which groups the fields together. I'm assuming you know how this is able to work on all the field in the group at the same time.

That said, using numbers and the field type for the field names is not syntactically incorrect, but it is stylistically unsound.

It would be better to use names like "Condition.balance", "Condition.mentalHealth", etc.

The conditional operator <condition>?<true option>:<false option>  is just shorthand for an "if/then" statement.  You'll find it listed in any core JS reference under operators.

event.target.value refers to the committed value of the field on which the event is occurring. i.e MouseUp happens after the value is committed to the check box

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

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
New Here ,
Nov 14, 2017 Nov 14, 2017

Copy link to clipboard

Copied

Awesome!

Thank you Sir! Your explination also clarifies how I can actually use the field names to my advantage. While I roughly understood the "dot" notation this makes it a great deal clearer.

Best Regards,

Dean

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 Beginner ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

How would I use this same structure, but not hide the option that is being checked.  Here is my current setup:

 

I have a java action setup for a checkbox called: "template.specialty_sport_aerobic"

 

This is the code:

this.getField("template").display = (event.target.value=="Off")?display.visible:display.hidden;

 

I have many checkboxes this applies to, but here are the names of two of them:

"template.specialty_sport_supersport"

"template.specialty_sport_polyathletic"

 

Here is what works great:

When I check "template.specialty_sport_aerobic", both of these are hidden

"template.specialty_sport_supersport"

"template.specialty_sport_polyathletic"

 

Here is my problem:

When I check "template.specialty_sport_aerobic", it also hides itself (and therefore cannot be unchecked).

Is it possible to add something to this script such that the box being checked will not be hidden?

 

Thank you!

 

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 ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

Add this line of code to the bottom of your script.

 

event.target.display = display.visible;

 

It re-displays the checkbox that is being checked. 

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

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 Beginner ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

Thank you so much!

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 Beginner ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

LATEST
Thanks so much!



*(425) 486-4292 <%28425%29%20486-4292>*

PacificOrthotic.com
FAX: (425) 486-3553

21828 87th Avenue SE #C1
Woodinville, WA 98072

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