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

Show/hide labels/text boxes with a checkbox

Explorer ,
Sep 20, 2024 Sep 20, 2024

Warning I know just enough to screw things up!

 

Looking to hide 3 fields based on the condition (checked or unchecked) of a checkbox.

 

Here are the fields:

  • cb_homeage (checkbox)
  • tb_yearbuilt (static label)
  • tb_homeyear (dynamic, inputs for the year it was built)
  • tb_homeage (calculated based on today's date - tb_homeyear)

 

If cb_homeage is checked, the three texboxes (tb_...) would be hidden

 

Here is what I have that is absolutely not working.

 

cb_homeage/Actions/Mouse Up/run javascript 

 

var fieldHide = event.target.isBoxChecked(0)?display.visable:display.hidden;

 

this.getField("tb_yearbuilt").display = fieldHide;
this.getField("tb_homeyear").display = fieldHide;
this.getField("tb_homeage").display = fieldHide;

 

One thing I am not sure about it the (and I'm sure I'm using the wrong terminology) checkbox identifier - so ...isBoxChecked(0) - not sure it's 0 (there are +/- 100 checkboxed and radio buttons)

 

TOPICS
How to , JavaScript , Modern Acrobat , PDF , PDF forms
1.9K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Sep 22, 2024 Sep 22, 2024

The error is because cb_homeage is not defined (when there's no quotes around it it's a variable that hasn't been defined).  It should be replaced with 0 to determine whether the box is checked in the Mouse Up action.  You are also calling field names that do not exist in the rest of the script so there will more errors.  However, since the checkbox "cb_homeage" has no other copies it's a lot easier to use the following script:

var fieldHide=event.target.value=="Off"?display.hidden:display.visible;

//set display of your fields to fieldHide, example:

this.getField("tb_homeage").display = fieldHide;

View solution in original post

Translate
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 ,
Sep 20, 2024 Sep 20, 2024

The zero is for the first "widget" of the field, meaning the first in a series of check boxes that are named identically.  The widget numbers are in the order the fields were created starting with zero.  If this check box is the only one, then the zero would be correct to identify this check box.  However, your error is that you mispelled visible.

Translate
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
Explorer ,
Sep 20, 2024 Sep 20, 2024

And that was a copy and paste :O/!!

 

How do I find the actual "ID" number for that check box?? There are 35+ check and radio button on this form page 

Translate
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 ,
Sep 20, 2024 Sep 20, 2024

In Prepare Form mode click the check-box you're interested it and it will be selected in the Fields list on the right. The number after "#" in that list is its index number in the group (note the numbers start from 0, not 1):

 

try67_0-1726869200889.pngexpand image

 

Translate
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
Explorer ,
Sep 21, 2024 Sep 21, 2024

@try67 - unfortunately I rename these as I'm going along? 

 

Is there a scripting way to find it?

 

checkbox.jpgexpand image

Translate
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 ,
Sep 21, 2024 Sep 21, 2024

If there's no # plus number in the fields panel, there is only one widget, so you would use the zero as you did originally.

Translate
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 ,
Sep 21, 2024 Sep 21, 2024

Yes. Look at the page property of the field. If it's a number there's only one copy of it; If it's an array, there are multiple copies.

Translate
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
Explorer ,
Sep 22, 2024 Sep 22, 2024

Sorry, still confused (not uncommon!). This is a 10 page form with 50+ checkboxes and 100+ radio buttons.

 

This page (page 1) has 2 checkboxes. I tried var fieldHide = event.target.isBoxChecked(0) & var fieldHide = event.target.isBoxChecked(1) - neither work.

 

Sure I'm missing something simple!?

 

Hamberg_0-1727013069127.pngexpand image

 

Translate
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
Explorer ,
Sep 22, 2024 Sep 22, 2024

Side note - here is the error that is being thrown: 

ReferenceError: cb_homeage is not defined

1:AcroForm:cb_homeage:Annot1:MouseUp:Action1

Translate
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
Explorer ,
Sep 22, 2024 Sep 22, 2024

Sorry, I was trying to call the check box by name in the error above

here is the error with isBox Checked(0):

TypeError: this.getField(...) is null
3:AcroForm:cb_homeage:Annot1:MouseUp:Action1

Translate
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 ,
Sep 22, 2024 Sep 22, 2024

Your field is called "cb_homeage", not "cb_homepage"... Hence the error message you're getting.

Translate
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
Explorer ,
Sep 22, 2024 Sep 22, 2024

@try67 I am not calling "cb_homepage" anywhere - what are you seeing that I'm not??

Translate
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 ,
Sep 22, 2024 Sep 22, 2024

From the error message you posted... But you will need to post the full code (and/or the file) for further help with this.

Translate
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
Explorer ,
Sep 22, 2024 Sep 22, 2024

Here is the whole document. Just did the page layouts and am trying to get page 1 figured out before fixing naming convenetions and such. Thanks in advance for the insight/help! 

Translate
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 ,
Sep 22, 2024 Sep 22, 2024

The error is because cb_homeage is not defined (when there's no quotes around it it's a variable that hasn't been defined).  It should be replaced with 0 to determine whether the box is checked in the Mouse Up action.  You are also calling field names that do not exist in the rest of the script so there will more errors.  However, since the checkbox "cb_homeage" has no other copies it's a lot easier to use the following script:

var fieldHide=event.target.value=="Off"?display.hidden:display.visible;

//set display of your fields to fieldHide, example:

this.getField("tb_homeage").display = fieldHide;

Translate
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
Explorer ,
Sep 23, 2024 Sep 23, 2024

Thank you! Something I didn't realize.. Once Javascript his an error the script stops? Is there anything like an: onerror.resume?

Translate
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 ,
Sep 23, 2024 Sep 23, 2024

- Yes.

- No, unless you catch the error in a try-catch clause, which you shouldn't really do. You should solve it.

Translate
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 ,
Sep 23, 2024 Sep 23, 2024
LATEST

Correct.  All the scripts stop when an error is triggered.  That's why you'll see "did you check the console for errors" a lot in the repsonses in this forum.  When someone uploads a form and says "it doesn't work" the first thing I do is clear the console.   Next I trigger a calculation, then I check the console.  It's a good way to trouble shoot.  As @try67 said, you can use try/catch, but all that will do is skip the error and you won't be able to find it.

Translate
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