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

Checkbox auto fill based on other fields

Community Beginner ,
Jan 23, 2019 Jan 23, 2019

Doing a step-by-step form and want a checkbox to auto populate once all fields in a step are completed. Can you autofill (check) a check box when more than five fields need to be filled in first? If not, how would I complete with text that says "YES" instead?

TOPICS
PDF forms
6.3K
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
1 ACCEPTED SOLUTION
Community Expert ,
Jan 23, 2019 Jan 23, 2019

The posted code is structural, not an actual script. So there are some issues that have to be dealt with before writing a actual script:

1)  What does completed mean?

2)  How to handle large numbers of fields, since my simple script puts it all in a single 'if'

So...

1) Completed usually means that the current value is different from the default value.

2) The easy way to handle a large number of arbitrary fields is to

     a) put the names in an array and loop over them

     b) Give them all group names using dot notation, so they can be acquired as a group.

The a) option is good for a single set of fields.

The b) option works in all situations and is particularly good for handling more than one section. I like this one best.

So, if the fields are named "Group.Territory", "Group.Region", etc.

Then this code will test for completion

var flds = this.getField("Group").getArray();

var bComplete = false;

if(flds.every(function(a){return a.value != a.defaultValue;}))

  this.getField("Section1Check").value = "Yes";

else

    this.getField("Section1Check").value = "Off";

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

View solution in original post

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 ,
Jan 23, 2019 Jan 23, 2019

Yes, that's possible. What are the names of the fields involved?

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 Beginner ,
Jan 23, 2019 Jan 23, 2019

There are 15 fields: two dropdown menus ("Territory" and "Region"), 12 text boxes ("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Email1", "Email2", "Email3", "Email4", "Email5", "Email6") and one checkbox ("1Familiarize").

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 ,
Jan 23, 2019 Jan 23, 2019

And how should it work, exactly?

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 ,
Jan 23, 2019 Jan 23, 2019

The posted code is structural, not an actual script. So there are some issues that have to be dealt with before writing a actual script:

1)  What does completed mean?

2)  How to handle large numbers of fields, since my simple script puts it all in a single 'if'

So...

1) Completed usually means that the current value is different from the default value.

2) The easy way to handle a large number of arbitrary fields is to

     a) put the names in an array and loop over them

     b) Give them all group names using dot notation, so they can be acquired as a group.

The a) option is good for a single set of fields.

The b) option works in all situations and is particularly good for handling more than one section. I like this one best.

So, if the fields are named "Group.Territory", "Group.Region", etc.

Then this code will test for completion

var flds = this.getField("Group").getArray();

var bComplete = false;

if(flds.every(function(a){return a.value != a.defaultValue;}))

  this.getField("Section1Check").value = "Yes";

else

    this.getField("Section1Check").value = "Off";

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
Community Beginner ,
Jan 23, 2019 Jan 23, 2019

Yes, completed would mean any option other than default.

This is all a bit above my head as I'm just getting familiar with code in Adobe (this is a request at work).

My understanding of this is:

- group all fields using their names - a singular name (Step1) followed by . (Such as "Step1.Name1")

- Name the checkbox in question "Section1Check", in the case of the code below.

Then use the following code in the Section1Check :

var flds = this.getField("Step1").getArray();

var bComplete = false;

if(flds.every(function(a){return a.value != a.defaultValue;}))

  this.getField("Section1Check").value = "Yes";

else

    this.getField("Section1Check").value = "Off";

This would mean I could do the same thing for Section 2 checkbox (name Step 2's fields "Step2.2Dropdown" etc then use the same code except "Section2Check" instead?)

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 ,
Jan 23, 2019 Jan 23, 2019

Correct!!  But start off simple to make sure you've got the details worked out.

You can put the code for all the sections into the same text field calculation script. 

As an advanced extension to this technique. The code could be placed in a document script as a function, where the name of the checkbox and the section name are passed into the function. Then the calculation script calls the function, rather than repeating the code. But this is unnecessary. It just make the code neater and easier to maintain.

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
Community Beginner ,
Jan 23, 2019 Jan 23, 2019

Thank you so much for your help! I got it working and feel like a magician

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 ,
Jan 23, 2019 Jan 23, 2019

You could do this with either a text field or a check box.  With a text field you'd use a custom calculation script to monitor the dependent fields. like this:

if((this.getField("Field1").value == "something") && (this.getField("Field2").value == "somethingelse"))

    event.value = "Yes";

else

    event.value = "No";

For a checkbox you'll need to use the same technique. However, checkboxes don't have an easily accessible calculation script, so the trick is to use hidden text field for the calculation;

if((this.getField("Field1").value == "something") && (this.getField("Field2").value == "somethingelse"))

    this.getField("Section1Check").value = "Yes";

else

    this.getField("Section1Check").value = "Off";

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 ,
Jan 20, 2023 Jan 20, 2023

I'm trying to understand this. I need to do this on my fillable form also. When a user enters text in a text field. I need the check box to check. If blank it's unchecked.   Can sone one help me more on this. Do I enter the code in the text field? The check box does not have the option.

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 ,
Jan 21, 2023 Jan 21, 2023

There are many ways to make changes in one field from data entered into another field.  But if the relationship is one to one, then one of the best strategies is to put the script that makes the changes into the field that is being changed. The reason for this is that it is easy and automatic to detect changes in a field value, from a script on that field.

 

So, put the script into the text field. To be specific, add this code to the custom Validation script on the Text Field.

 

this.getField("Checkbox").value = (event.value == "")?"Off":"Yes";

 

There are two things in this code that you may need to change for your form.

1.  Change "Checkbox" to the real name of the checkbox on your form.

2. If necessary, change "Yes" to the real export value of the checkbox on your form. 

 

 

 

 

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 ,
Mar 08, 2023 Mar 08, 2023

Thank you so much. Now if I can just figure out the code to hide layer based on check box. The option to show hide layers is not working. 

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 ,
Mar 08, 2023 Mar 08, 2023

If you are having an issue with "layers", then make a new forum post.  

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
Explorer ,
Sep 13, 2023 Sep 13, 2023

Is there a way to calculate any value that is greater than zero in the "something" spot? I would also like to know if there's a way to use a range of values. For example, if Field1 is any value greater than 0 and Field2 is between 1 and 5 then Field 3 is 1.

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 20, 2023 Sep 20, 2023
LATEST

Yes, read this:

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

 

If you have further issues, then please post a your question to a new thread, with a complete explanation. 

 

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