2

How are the hyperref package options onchange and onkeypress used?

In the following example, which transfers the input-text to the output-text, only the pushbutton using the onclick -option work.... but not the onchange option within the textfield.

I would like to obtain that the input text is copied to the output text field immediately, if the input text field changes, WITHOUT using the push button.

\documentclass[20pt,a4paper]{article}

\usepackage{xcolor}
\usepackage[]{hyperref}

%% Short hand commands
\newcommand{\textforlabel}[2]{%
   \TextField[name={#1}, value={#2}, width=9em,align=2,%
   bordercolor={0.990 .980 .85},%
   readonly=true]{}%
   }%

\newcommand{\heading}[1]{\textsc{#1}}

%% document
\begin{document}
\begin{Form}

%% Input
\heading{Input}
\\
\\
\textforlabel{l01}{Input:}
\TextField[name=input1,onchange={%
 var datastring1=this.getField("input1").value;
 this.getField("output1").value=datastring1;%
 }%
,width=2in, bordercolor={0.650 .790 .94}]{}%
\\
%% Push button
\PushButton[name=button1,onclick={%
 var datastring1=this.getField("input1").value;
 this.getField("output1").value=datastring1;%
 }%
 ,bordercolor={0 0 0}]{To Output}
 \\ 

%% Output
\heading{Output}
\\
\\
\textforlabel{l02}{Output:}
\TextField[name=output1,width=2in,%
 bordercolor={0.650 .790 .94},readonly=true]{}

\end{Form}
\end{document}
  • pdflatex issues a message: "Package hyperref Warning: HTML field option 'onchange' is ignored on line 28". Possibly this functionality is not provided for PDF forms, and only for HTML forms (which you can make with htlatex)? – Marijn Mar 30 '17 at 22:21
1

You can use the keystroke option of \TextField and evaluate the event.value property in the JavaScript to set the output field's value.

Unfortunately, event.value contains only the value that was known before input1 received the keystroke. You have to end the input by hitting "Enter" in order to also get the last key typed in:

\documentclass[20pt,a4paper]{article}

\usepackage{xcolor}
\usepackage[]{hyperref}

%% Short hand commands
\newcommand{\textforlabel}[2]{%
   \TextField[name={#1}, value={#2}, width=9em,align=2,%
   bordercolor={0.990 .980 .85},%
   readonly=true]{}%
   }%

\newcommand{\heading}[1]{\textsc{#1}}

%% document
\begin{document}
\begin{Form}

%% Input
\heading{Input}
\\
\\
\textforlabel{l01}{Input:}
\TextField[name=input1,keystroke={%
% var datastring1=this.getField("input1").value;
% this.getField("output1").value=datastring1;%
  this.getField("output1").value=event.value;
}%
,width=2in, bordercolor={0.650 .790 .94}]{}%
\\
%%% Push button
%\PushButton[name=button1,onclick={%
% var datastring1=this.getField("input1").value;
% this.getField("output1").value=datastring1;%
% }%
% ,bordercolor={0 0 0}]{To Output}
% \\ 

%% Output
\heading{Output}
\\
\\
\textforlabel{l02}{Output:}
\TextField[name=output1,width=2in,%
 bordercolor={0.650 .790 .94},readonly=true]{}

\end{Form}
\end{document}
  • 1
    Thanks. Very Helpful. The restriction that the last letter is not transfered, will be difficult to overcome as I see. I don t think that it is possible. – JacobHer Mar 31 '17 at 13:47
0

Would this be enough for you ? It is not completely instaneous but changes the whole answer directly. You have to press tab or go in another field to implement the JS command, similarly as with Keystroke, which does not implement the last letter until after the event.

    \documentclass[20pt,a4paper]{article}

    \usepackage{xcolor}
    \usepackage[]{hyperref}

    %% Short hand commands
    \newcommand{\textforlabel}[2]{%
       \TextField[name={#1}, value={#2}, width=9em,align=2,%
       bordercolor={0.990 .980 .85},%
       readonly=true]{}%
       }%

    \newcommand{\heading}[1]{\textsc{#1}}

    %% document
    \begin{document}
    \begin{Form}

    \heading{Input 1}
    \\
    \\
    \textforlabel{l01}{Input 1:}
    \TextField[name=input1,width=2in, bordercolor={0.650 .790 .94}]{}%
    \\
    %% Push button
    \PushButton[name=button1,onclick={%
     var datastring1=this.getField("input1").value;
     this.getField("output1").value=datastring1;%
     }%
     ,bordercolor={0 0 0}]{To Output}
     \\ 

    %% Output
    \heading{Output 1}
    \\
    \\
    \textforlabel{l02}{Output1:}
    \TextField[name=output1,width=2in,%
     bordercolor={0.650 .790 .94},readonly=true]{}

    \vspace{40pt}

    %% Input 2
    \heading{Input 2}
    \\
    \\
    \textforlabel{l03}{Input 2:}
    \TextField[
      name=input2,
      width=2in,
      bordercolor={0.650 .790 .94}]{}
    \\

    %% Output 2
    \heading{Output 2}
    \\
    \\
    \textforlabel{l04}{Output:}
    \TextField[
      name=output2,
      width=2in,
      bordercolor={0.650 .790 .94},
      readonly=true,
      calculate = {%
        event.value = this.getField("input2").value;
      }
      ]{}
    \end{Form}
    \end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.