Hide/show fields with checkbox or radio buttons

I have tried the properties to show hide and it is not working. Also tried a script.

I have 4 fields set to hidden, if user selects, the fields are visible. (only 2 in code now).

var f = this.getField("chk2");
if(f.isBoxChecked(0))
this.getField("teamset2").display="visible"
this.getField("member2").display="visible"
else
this.getField("teamset2").display="hidden"
this.getField("member2").display="hidden"


I also tried the properties of show/hide. using On blur and on focus. When you check the box, the fields are visible, but when you click anywhere else they disappear, even tho the check mark is still there.

If check is there, visible, if you uncheck they should hide.


Murray James


3 Answers

Voted Best Answer

Your code has some problems. It should be something like this, if you are creating a form with Adobe Acrobat:

var f = this.getField("chk2");
if(f.isBoxChecked(0)){
    this.getField("teamset2").display = display.visible; 
    this.getField("member2").display = display.visible; 
}
else {
    this.getField("teamset2").display = display.hidden;
    this.getField("member2").display = display.hidden;
}

Are you creating your form with Acrobat or LiveCycle Designer?


By Almir R V Santos   

It may be a personal preference, but I don't have much trust in the isBoxChecked() method. If it is just a single checkbox, using the value of the field works, and is more reliable (IMHO). For that, you'd add the following code to the MouseUp event of the checkbox:

if (event.target.value != "Off") {
// box is checked
this.getField("teamset2").display = display.visible ;
this.getField("member2").display = display.visible ;
} else {
// box is unchecked
this.getField("teamset2").display = display.hidden ;
this.getField("member2").display = display.hidden ;
}

Note that's for Acroforms. If you happen to have XFA, the syntax is different.

If there are various options for the return value (meaning multiple checkboxes being grouped with same field name but different return values, those values can be evaluated using the switch… statement.

If you are using hierarchical field names for the fields controlled via the checkbox, you can use the group name, and toggle the display property for the whole group in one single line.

Hope this can help.

Max Wyss.


Max Wyss   

Change all instances of "visible" to display.visible; and all instances of "hidden" to display.hidden;

___________________________________________________________________________________________________________________________________________________________________________

David Dagley, CFP®
Developer at

www.PDFAutomationStation.com

IMAGE ALT TEXT IMAGE ALT TEXT IMAGE ALT TEXT

_________________________________________________________________________________________________________________


David Dagley   


Please specify a reason: