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

Clear text field when checkbox is unchecked

Guest
Jan 12, 2017 Jan 12, 2017

Hello,

I'm trying to write some JavaScript for new forms at work.  I've been able to write a code so that when a checkbox is checked, the information from one set of fields copies over to another:

if(getField("Check Box12").value!="On")

this.getField("Applicant's Name").value = this.getField("Property Owners Name").value;

this.getField("pertyAddress").value = this.getField("Address").value;

this.getField("Contact Name1").value = this.getField("Contact Name").value;

this.getField("Phone1").value = this.getField("Phone").value;

this.getField("Postal Code1").value = this.getField("Postal Code").value;

this.getField("Cell Phone1").value = this.getField("Cell Phone").value;

this.getField("Email1").value = this.getField("Email").value;

What I'm trying to do now is if that checkbox is unchecked, have the text fields clear and reset to blank.  Right now I'm trying to use this and it is not working:

if(getField("Check Box12").value!="False")

this.resetForm("Applicant's Name")

this.resetForm("pertyAddress")

this.resetForm("Contact Name1")

this.resetForm("Phone1")

this.resetForm("Postal Code1")

this.resetForm("Cell Phone1")

this.resetForm("Email1")

Any suggestions?

TIA!

TOPICS
Acrobat SDK and JavaScript , Windows
7.1K
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

correct answers 1 Correct answer

Community Expert , Jan 12, 2017 Jan 12, 2017

Couple of notes.

- To execute more than one line of code after an if-statement you have to use curly brackets around the whole code block.

- The default value of a check-box that isn't ticked is "Off", not "False".

- You can reset multiple fields in a single command by putting then in an array as the parameter of the resetForm method.

- I believe you defined your conditions the other way around. The "!=" operator means NOT EQUALS. You probably want to use the EQUALS operator, which is "==".

So combin

...
Translate
Community Expert ,
Jan 12, 2017 Jan 12, 2017

Couple of notes.

- To execute more than one line of code after an if-statement you have to use curly brackets around the whole code block.

- The default value of a check-box that isn't ticked is "Off", not "False".

- You can reset multiple fields in a single command by putting then in an array as the parameter of the resetForm method.

- I believe you defined your conditions the other way around. The "!=" operator means NOT EQUALS. You probably want to use the EQUALS operator, which is "==".

So combining it all your code should be something like this:

if (this.getField("Check Box12").value=="Off") {

    this.resetForm(["Applicant's Name", "pertyAddress", "Contact Name1", "Phone1", "Postal Code1", "Cell Phone1", "Email1"]);

} else {

    this.getField("Applicant's Name").value = this.getField("Property Owners Name").value;

    this.getField("pertyAddress").value = this.getField("Address").value;

    this.getField("Contact Name1").value = this.getField("Contact Name").value;

    this.getField("Phone1").value = this.getField("Phone").value;

    this.getField("Postal Code1").value = this.getField("Postal Code").value;

    this.getField("Cell Phone1").value = this.getField("Cell Phone").value;

    this.getField("Email1").value = this.getField("Email").value;

}

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
Guest
Jan 12, 2017 Jan 12, 2017

Thank-you very much! That worked perfectly.

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 ,
Jun 28, 2022 Jun 28, 2022

I am trying to do something similar, however, I have just one field to clear.

My checkbox ("D1") when On is to to copy one field (Day) to another (DN), but when off the field (DN) is to be cleared.

 

I have tried using:

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

this.resetForm(“DN”);

} else {

this.getField("DN").value = this.getField("Day").value;

}

but I keep getting a syntax error at the else statement, what am I doing wrong some help would be fantastic thanks.

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 ,
Jun 29, 2022 Jun 29, 2022

When you get an error, always say what it is, exactly...

However, in this case I can see it quite easily. It's in this line:

this.resetForm(“DN”);

You're using curly quotes instead of "straight" ones. This usually happens when people use Word for writing the code, which you should never do. Use a plain-text editor only, like Notepad++.

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 ,
Jun 29, 2022 Jun 29, 2022

Thank you soooo much that worked "of course" 🙂 I will remember note to use Word going forward.

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
New Here ,
Sep 22, 2022 Sep 22, 2022

Hi - I have a mix of checkboxes and textfields that are dependent on a main Checkbox. I tried the above to clear the textfield but it does not seem to be working. Would you be able to advise what I might be doing incorrectly?

 

(i.e. unchecking 'Checkbox1' should clear 'checkboxA' 'checkboxB' 'Textfield1' 'Textfield2' etc.)

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, 2022 Sep 22, 2022

We can't help if you don't say exactly what you did (which code you used, where you put it), and what the results were, or share the file in question with us.

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
New Here ,
Sep 23, 2022 Sep 23, 2022

see file attached. Ive set two javascript (one for the checkbox and one for the textfield) previously I only had one javascript and that also was not working. The checkboxes clear when checkbox1 or checkbox2 are unchecked but textfields do 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 23, 2022 Sep 23, 2022

Did you check the console window (Ctrl-J) for errors?

Here's a video on how to use the console window. 

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Sep 23, 2022 Sep 23, 2022

Unfortunately it shows this feature is not included in my acrobat license version that I have at work

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

The JavaScript Console Window is available in all versions and variations of Acrobat, even the free Reader. 

One way to get the console to display is to set the "Show console on errors and messages" option in the Acrobat Preferences.  Below is a screenshow of the Reader Preferences dialog. 

 

JSPrefs_ShowConsole.jpg

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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