Skip to main content
New Participant
November 10, 2017
Answered

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

  • November 10, 2017
  • 1 reply
  • 5904 views

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...

This topic has been closed for replies.
Correct answer Thom Parker

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;

1 reply

Thom Parker
Thom ParkerCorrect answer
Adobe Expert
November 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;

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
New Participant
November 14, 2017

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

Thom Parker
Adobe Expert
November 14, 2017

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