# Hide LaTeX parts through JavaScript

I'm creating an interactive pdf form with the hyperref package. The form is quite comprehensive, so the current goal is to only show parts that are relevant to the specific user. For example, the user indicates that he is male --> only the items that concern males are displayed.

Usually it's not difficult to display only certain parts of a document, e.g. using the ifthen package:

\documentclass{article}
\usepackage{ifthen}

\begin{document}
\newboolean{boolvar}
\setboolean{boolvar}{false}
\ifthenelse{\boolean{boolvar}}{show if true}{show if false}
\end{document}


I'm lost, however, when it comes to combining variables like that with user input from form fields. Using JavaScript and the insdljs package, I've gotten as far as hiding form fields, but not text outside of these form fields: (Note: insdljs is not part of TeX Live, but available here)

\documentclass{article}
\usepackage[pdftex]{hyperref}
\usepackage[pdftex]{insdljs}

\begin{insDLJS}{mydljs}{some comment}
function hide(){
if(this.getField("somefield").display == display.visible){
this.getField("somefield").display = display.hidden;
} else{
this.getField("somefield").display = display.visible;
}
}
\end{insDLJS}

\begin{document}
\begin{Form}
\PushButton[onclick={hide();}]{Hide/Show}\\
\TextField[name=somefield, width=4em]{Any way to hide e.g. this text?}
\end{Form}
\end{document}


Hiding form fields like that is very handy, but I'd like to apply that to entire tables or sections. Any idea? Thanks!

-
I can't help but I'm probably not the only person who would love to see whatever you come up with! – Andy Clifton Jun 1 '13 at 14:24
@LostBrit Thanks, but what I've been able to dig up so far seems quite disheartening. For example, in a related post the accepted answer read If you want parts of the PDF to change based on form events, you pretty much have to make those changing things read-only text fields. The topic wasn't exactly the same, though. So there is still some hope! – igorr02 Jun 1 '13 at 17:44

I don't know actually, but I conducted small research, and think that you can't hide it, you can do nothing with it, at all (without a little trick, of course)

First of all, I create a simple example and open it in Adobe Acrobat. I see, that a field and a label independent. And there is no any TextField properties that can change the label. Textfield it's field, label it's text.

If you really wanna hide label, you can do something like this:

1. you hide field 'test', and 'tf1' is visible

2. field 'test' is visible, 'tf1' to hide

It works, but I don't sure, that you need it.

In order to verify that we can't change the text, I have played with the buttons. Using javascript API for Acrobat Reference I found buttonGetCaption and buttonSetCaption methods, to change label (seems like caption), it works not as I expected:

\PushButton[name=testButton1,onclick={
console.show();
console.println('-------------');
console.println('value is: '    +event.target.value);
console.println('caption is: '  +event.target.buttonGetCaption());
console.println('-------------');
event.target.buttonGetCaption()==''?event.target.buttonSetCaption('boom'):event.target.buttonSetCaption('');
}]{\ test\ }
\PushButton[name=testButton2,onclick={
event.target.buttonGetCaption()==''?event.target.buttonSetCaption('boom'):event.target.buttonSetCaption('');
}]{\ \ \ \ \ \ \ }\\[0.2cm] %I didn't find as to change width. Parameter 'width' work only for fields.


before click: after:

So, i think it's bad idea to hide text, but you can easily hide Textfield, lets do it!

\usepackage{hyperref}
\usepackage{calc}
\usepackage[pdftex]{insdljs}

\newlength{\mylen}
\settowidth{\mylen}{Enter name: }

\begin{insDLJS}{mydljs}{ }
this.getField('testLabel').borderColor=color.transparent;
function hide(){
if(this.getField("test").display == display.visible){
this.getField("test").display = display.hidden;
this.getField("testLabel").display = display.hidden;
} else{
this.getField("test").display = display.visible;
this.getField("testLabel").display = display.visible;
}
}
\end{insDLJS}
\begin{document}
\begin{Form}
\TextField[name=testLabel,value=Enter name:,width=\mylen,readonly]{} % I don't know why but width=\whidthof and width=\the\whidthof don't work :(
\TextField[name=test]{} \\
\PushButton[onclick={hide();},
]{hide}\\[0.2cm]
\end{Form}
\end{document}


before:

after:

Another solution of this problem to replace labels with defaultValue, example:

\TextField[name=test2,default=Enter something,
onfocus={
if(event.target.value==event.target.defaultValue)event.target.value='';
},
onblur={
if(event.target.value=='')event.target.value=event.target.defaultValue;
}
]{}


before click:

after:

You can hide it as before.

-