These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Checkbox used to hide templates depending on if field is empty

Chellie
Registered: Jan 21 2011
Posts: 6
Answered

Hi everyone!
 
I am using a JS code to show/hide template pages using a checkbox, but if a user unchecks the box or checks it again, the template page keeps appearing and appearing. I'd like to hide the page if the box is unchecked AND the page is empty.
 
Here is the code I'm using:
 
var sName = "UM_Master";var a = this.templates;for (i = 0; i < a.length; i++) {if(a[i].name == sName) {a[i].spawn(this.numPages, false, false);break;}}
 
First my variables are:
UM_Check = checkbox (located on page 1)
UM_Master = template (appears on page 2 if UM_Check is checked)
UMPg2_TXT = text field a user fills in on UM_Master
 
What I want to do is this:
When a user checks UM_Check, the UM_Master template appears on page 2 (which is the JS above). If a user made a mistake by choosing that option, the UM_Master template page should hide. (With the above JS, if a user unchecks the box a new UM_Master page appears.) Before the page is hidden, I want JS to first make sure nothing was written in UMPg2_TXT on UM_Master.
 
So.....
If UM_Check is unchecked AND if the UMPg2_TXT field is empty on UM_Master, then hide UM_Master; else, if UM_Check is unchecked AND if the UMPg2_TXT field is NOT empty, keep UM_Check checked and display warning, "Delete Text on UM_Master first before unchecking this box."
 
Is this possible? Can someone help me with this?
 
Thanks so much,
Chellie

My Product Information:
Acrobat Pro 9.3, Macintosh
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You might want to check the value of the check box and based on the value select the block of code to show the template or hide the template. A non-checked check box or radio button has a value or "Off".

George Kaiser

Chellie
Registered: Jan 21 2011
Posts: 6
Hi George,

Thanks for that. I actually do not have code beyond what I have above. I'm trying to figure out how to write it so the above scenario happens. And, can Adobe handle that level of JS code? This is my first time dealing with JS in Adobe.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Yes.

First hiding a template is only available with the full version of Acrobat, so none of your users can use Reader. If your users have Reader then you will need to apply Extended Reader Rights to spawn templates and delete pages.

var tMyTemplates = this.templates;
if(tMyTemplates[0].hidden == false) {
tMyTemplates[0].hidden = true; // hide template
this.resetForm(["UMPg2_TXT"]); // clear text
} else {
tMyTemplates[0].hidden = false; // show template
}


George Kaiser

Chellie
Registered: Jan 21 2011
Posts: 6
Hi George,

Yes, all people using this have Acrobat Pro. It's internal to our department, so that won't be a problem.

Thanks **so much** for your help. I will test it out and let you know what happens.

Chellie!
Chellie
Registered: Jan 21 2011
Posts: 6
Hi George,

I've tried this code and a few variations, and it didn't work. It keeps recreating the page, like a 'spawn(numpages)' action. Still trying to work it out.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
A page that gets created when you spawn a template is not itself a template page. If you want to hide it, you'll have to delete it (doc.deletePages).
Chellie
Registered: Jan 21 2011
Posts: 6
Hi George Johnson,

Actually, the checkCheckbox() function allows me to hide/unhide a page perfectly! The spawn page kept adding the page over and over, while in my case it's only going to be added once. So now the only thing I'm trying to do is add the code to check if the field is empty before hiding it.

This is what I have thus far:
function checkCheckbox(){
if (this.getField("myCheckbox").isBoxChecked(0) == true) {
this.getTemplate("myTemplate").hidden=false;
} else {
this.getTemplate("myTemplate").hidden=true;
}
}
checkCheckbox();
Chellie
Registered: Jan 21 2011
Posts: 6
Accepted Answer
Hi all,

I have figured out the code:

var f=this.getField("myField");
var s=(f.value.length>0);function checkCheckbox(){
if (this.getField("myField").isBoxChecked(0) == true) {
this.getTemplate("myTemplate").hidden=false;
} else {
while (f.value.length>0) {
this.getField("myField").value = "Yes";
app.alert("You cannot deselect the form while text is in the fields. Please delete the text then deselect.",1,1);
this.getTemplate("myTemplate").hidden=false;
break;
}
while (f.value.length==0) {
this.getTemplate("myTemplate").hidden=true;
break; }
}
}
checkCheckbox();

Thanks so much for all of your help!