toggle checkbox to delete text field

hi expert,
i've been working for this for three hours using Acrobat and i can't get it right.
i have
check box1 and Text1

here is my code on checkbox1

if(event.target.lastVal == event.target.value)
{
event.target.lastVal = null; event.target.value = "Off";
this.getField("Tex1").value = "";
}

what i am trying to do is if checkbox1 is unchecked or OFF, it will clear "Text1".

please help me again here.. thank you very much.


Marvin G.


6 Answers

Voted Best Answer

Ok, I'll add my 2 cents. My impression is that you want this functionality

  • Checkbox "On" - User can enter data into the field
  • Checkbox "Off" - Field cleared and user is unable to enter data.

The easy code is what Max provided, use the check box Mouse Up to set the ReadOnly property of the field and to clear it when necessary. But this does not provide a general solution. There are still situations where the field can filled, and/or the check box changed that do not trigger the correct behavior.

The complete solution is to use either a calculation script on the field or the "Keystroke/WillCommit" script on the check box, because these events are activated in all situations. Unfortunately its a pain to enter a keystroke script into a checkbox, so the calculation script on the text box is the winner.

Here is a correction of Almir's script:

// In text field
if(this.getField("Check1").value == "Off")
{
   event.value = "";
   event.target.readonly = true;
}
else
   event.target.readonly = false;

It is important to always set the field value with "event.value" in a calculation.

Now, here is a much simpler and trickier script that does the same thing

event.rc = (this.getField("Check1").value == "Off");
event.target.readonly = event.rc;
event.value = "";

Thom Parker
The source for PDF Scripting Info pdfscripting.com
All About PDF Stamps in Acrobat and Paperless Workflows - THE BOOK !!

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work


By Thom Parker   

There is no lastVal property of the event object (at least not in Acrobat JavaScript; if you are using LiveCycle Designer, then anything is possible…).

The normal way to clear a field when an associated checkbox is unchecked is like this (this code will be placed in the MouseUp event of the checkbox):

if (event.target.value == "Off") {
this.getField("Text1").value = "" ;
}

And that should do it.

Very often, the field Text1 would be shown and hidden as well. The script would look like this in that case:

if (event.target.value == "Off") {
this.getField("Text1").value = "" ;
this.getField("Text1").display = display.hidden ;
} else {
this.getField("Text1").display = display.visible ;
this.getField("Text1".setFocus() ;  // optional, sets the focus to the field, makng it ready for entry
}

Hope this can help.

Max Wyss.


Max Wyss   

thanks. how about when checkbox1 was turned OFF by other script, Text1 was not cleared to value="" since i did not clicked off the checkbox1 whic is the trigerring of the code.

so if radiobutton turn off checkbox1 automatically, the code on checkbox1 to set "Text1" does not execute since it was not mouse up.


Marvin G.   

I would use the following script as a Custom calculation script in Text1 field:

var cbv = this.getField("checkbox1").value;
var f = event.target;

if (cbv == "Off") {
    f.value = "";
    f.readonly = true;
}
else {
    f.readonly = false;
}

Don't forget to remove the code from "checkbox1" field and make sure to type checkbox1 name exactly as is in the form, considering case.


Almir R V Santos   

I the checkbox itself is controlled via a script, you would replace the line

if (event.target.value == "Off") {

with

if (this.getField("checkbox1").value == "Off") {

and the script works again. Note that this modified script would be in the Calculate event of any other field, except Text1. If it were assigned to Text1, look at the other answer you received.

Hope this can help.

Max Wyss.


Max Wyss   

thank you SO MUCH Thom, Max and Almir.

now i got my desired behavior. :)

one more question on Thom's elegant code

event.rc = (this.getField("Check1").value == "Off");event.target.readonly = event.rc; 
event.value = "";


how can i enter text again to Text1.

because when i turned off check box 1, both checkbox1 and text1 were disabled.

thanks guys.


Marvin G.   


Please specify a reason: