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

Need Help: I need a check box to auto fill a set value in separate text field.

Guest
Dec 27, 2011 Dec 27, 2011

Hi, this is my first time with inserting scripts into a PDF Form.

We have a form that requires the user to check a box regarding medical conditions they have (i.e. Asthma, Diabetes, Hypertension).  Later in the form, they have to type out the same condition into a text box. (I know stupid to double document, but it is required).  Is there a script that will auto fill the text box when they check the box?  For example, if they check asthma, the text box on a later page in the PDF will say "Asthma, controlled."  Is this possible?

In addition, if they don't click Asthma, and do click Diabetes, could it add Diabetes to the first text box, but if they check both, it would add Asthma to box 1 and Diabetes to box 2?

Any help would be GREATLY appreciated.

I'm using Acrobat 9

Thanks!

TOPICS
PDF forms
28.9K
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
New Here ,
Dec 30, 2011 Dec 30, 2011

You might try entering this scipt in the textbox that is to contain the word "Asthma" where appropriate.

The script is based on checkboxes with names "cbAsthma" and "cbDiabetes"

The script as it stands adds all the text in one box.

As always with these scripts they are case sensitive.

var a =""

if (this.getField("cbAsthma").value == "Yes")
{
a="Asthma (controlled) "
}

if (this.getField("cbDiabetes").value =="Yes")
{
a=a + "Diabetes"
}

event.value=a

Hope this is somewhere near what you are looking for

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
LEGEND ,
Dec 27, 2011 Dec 27, 2011

You will have to add a custom script. controlling which text form field in order of selection or re-selection might become quite complex. It might be easier to use one text box with multiple lines.

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
Dec 27, 2011 Dec 27, 2011

Thanks.  What script would I use for the one text box?

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 ,
Dec 29, 2011 Dec 29, 2011

I did not understand the last part, but to fill the same way any two fields would be enough to give it the same name?

For example, the checkbox "Asthma" copied and pasted into two separate pages, if checked will give the same result in both.

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
Dec 29, 2011 Dec 29, 2011

I need it to populate text into a text box, not check another box.  When the user checks the checkbox for Asthma, I want it to insert the text "Asthma" into a textbox located in a later section of the form.  Any ideas on the JS code that will make this 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
New Here ,
Dec 30, 2011 Dec 30, 2011

You might try entering this scipt in the textbox that is to contain the word "Asthma" where appropriate.

The script is based on checkboxes with names "cbAsthma" and "cbDiabetes"

The script as it stands adds all the text in one box.

As always with these scripts they are case sensitive.

var a =""

if (this.getField("cbAsthma").value == "Yes")
{
a="Asthma (controlled) "
}

if (this.getField("cbDiabetes").value =="Yes")
{
a=a + "Diabetes"
}

event.value=a

Hope this is somewhere near what you are looking for

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
Dec 30, 2011 Dec 30, 2011

This is exactly what I'm needing. Thank you SO much!!!!

However, I can't get it to work.  This may be a dumb question, but when I enter the script in the text field properties, do I put it under the actions tab with a certain trigger, or do I put it under the format tab in the custom format script.   Also, when I'm naming the Asthma checkbox, do I need to name it "cbAsthma" or just cbAsthma?

Thanks again for the help!

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
LEGEND ,
Jan 01, 2012 Jan 01, 2012

If you want the entries on 2 different lines, then you will need to adjust  the entry with the new line character string, "\n", and make the text field a multi line field.

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 04, 2012 Jan 04, 2012

Ok, so I've been working the entire holiday weekend.  I have a sample of what I've been working on.  Could I e-mail it to you privately?  It has the code you gave put into the custom calculate script box, but when I ck the Asthma box, it doesn't put the text in the diagnosis 1 text box.   Any ideas?  Could you put in what needs to be there to get it to work and send it back to me?  Once I get it initially working, I will be able to populate out to all the other instances I need.   Thank you so much!

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
LEGEND ,
Jan 04, 2012 Jan 04, 2012

Diseases Summary is a sample with 3 possible solutions.

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 05, 2012 Jan 05, 2012

THANK YOU, THANK YOU, THANK YOU!!!!!  I got it to work :-).  This is Such a big help!!!!  My stupid mistake was that my checkbox export value was set to "on" instead of "yes".  Once I changed it, it worked like a charm.

So, in regards to the "new line string", where in the code do I put the "\n"  I'm trying a few places, but haven't had it go yet.

Also, is there any way in the sample you sent, that if both check boxes are checked, the first check shows up on the first text box, and the second check box shows up in the second text box, not both on the same line?  What about if the first check box is blank, but the second one is checked, could the second check box populate to the first text box instead of the second text box?

Again, thanks so much for your patience and the help!

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
LEGEND ,
Jan 05, 2012 Jan 05, 2012

You can change the separator string from ", " to "\n" for a new line. You may also need to set the "Multi-line" option to selected to have them show on each line.

As to using 2 text fields, you are making the scripting more complex as to when and were you are going to test the check boxes and fill the text boxes as needed. This will make the code more complex and harder to maintain by inexperienced users. I would create a document level function to populate clear the text boxes and repopulate the text boxes' values as each check box is selected.

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 ,
Nov 03, 2012 Nov 03, 2012

Hi,

I stumbled across this thread in looking for a solution to a very similar js issue I am having.

I have a long list of checkboxes in which a text field is populated as the boxes are checked. I modified the code provded for my needs and it worked for the two boxes I initially tried it out on. However, is there a simpler method of notation if you had 25 such checkboxes?

for example, can I modify the code as follows? I've tried a short version of this modified code, but it doesn't work quite right. Any teaching points would be appreciated.

var a =""

if (this.getField("checkbox 1").value == "Yes")
{
a="Text for box1 ";
}

if (this.getField("checkbox 2").value =="Yes")

b="Text for box2";

}

If (this.getField("checkbox 3").value=="Yes")

c="Text for box3:;

}

<and so on until checkbox 25>

a=a + b + c + d + ..... y
}

event.value=a

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
LEGEND ,
Nov 03, 2012 Nov 03, 2012

AustinCK,

You could simplify the code a lot if you set the export value of each checkbox to the text you want associated with it, as opposed to "Yes". The custom Calculate script for the text field could then be something like:

// Custom Calculate script

(function () {

    var i, v, s = "";

    // Loop through the 25 check boxes

    for (i = 1; i < 26; i += 1) {

        // Get the value of the current checkbox

        v = getField("checkbox " + i).valueAsString;

        // Concatenate check box value to string if not deselected

        if (v !== "Off") s+= v;

    }

    // Set this field value

    event.value = s;

})();

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 ,
Nov 04, 2012 Nov 04, 2012

Thank you, I will try your idea instead.  I think, from what I can gather in your code, that part of the trick is to use a logical system of naming the checkbox field? Random names would complicate the code.

So it is important to keep track of the information belonging to the checkboxes, and make sure they are in the right place.

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 ,
Nov 04, 2012 Nov 04, 2012

Hi,

Your code worked very nicely, thank you very much.

I have one other question. My assigned checkbox export "value" is a rather long sentence. When the boxes are checked, the sentences run-on one another. How can you specify to add 1 or 2 line breaks (/n) between each 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
LEGEND ,
Nov 04, 2012 Nov 04, 2012

Change this line:

if (v !== "Off") s+= v;

To:

if (v !== "Off") s+= v + "\r";

You'll be left with an extra carriage return at the end unless you remove it, but that may be OK. If you want it removed and or don't want it added in the first place and need help with doing that, post again.

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 ,
Jun 18, 2013 Jun 18, 2013

Hi, I'm staring at the code trying to make it work for my needs, which are similar to Austin's.

I have a 16-page form with 2,988 checkboxes in the entire document (a little over 200 in each page besides the first and last page). The last page contains a textbox labled Summary, and it should provide a summary of all the items checked in the document. Theoretically only one page will be populated at a time, so there will be no more than 200 checkboxes checked in the entire document.

Is it possible to use the same code? And do I just put the code under the text in the script that reads:

form1.SummaryPage.SummaryText::calculate - (JavaScript, client)

The JS I am trying to get to work is the following from a post above.

// Custom Calculate script

(function () {

    var i, v, s = "";

    // Loop through the 2988 check boxes

    for (i = 1; i < 2989; i += 1) {

        // Get the value of the current checkbox

        v = getField("checkbox " + i).valueAsString;

        // Concatenate check box value to string if not deselected

        if (v !== "Off") s+= v + "\r";

    }

    // Set this field value

    event.value = s;

})();

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
Sep 25, 2013 Sep 25, 2013

From this code:

// Custom Calculate script

(function () {

    var i, v, s = "";

    // Loop through the 25 check boxes

    for (i = 1; i < 26; i += 1) {

        // Get the value of the current checkbox

        v = getField("checkbox " + i).valueAsString;

        // Concatenate check box value to string if not deselected

        if (v !== "Off") s+= v + "\r";

    }

    // Set this field value

    event.value = s;

})();

How would I add additional text that is always there? For example, adding a period to the end of the generated text?

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
LEGEND ,
Sep 25, 2013 Sep 25, 2013

If you want to add additional text to the end of the string the the code generates, you could do something like:

event.value = s + "additional text goes here.";

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
Sep 25, 2013 Sep 25, 2013

hmmm, actually I realized I want to be able to edit the formed text, is that possible with the above code? Or do I have to go a totally different method?

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 ,
Sep 18, 2015 Sep 18, 2015

George

I saw this code of yours in one of the post

// Custom Calculate script

(function () {

    var i, v, s = "";

    // Loop through the 25 check boxes

    for (i = 1; i < 26; i += 1) {

        // Get the value of the current checkbox

        v = getField("checkbox " + i).valueAsString;

        // Concatenate check box value to string if not deselected

        if (v !== "Off") s+= v;

    }

    // Set this field value

    event.value = s;

})();

I have a form with 10 check boxes that are named CheckBox, ChckBox1, CheckBox2........you get the idea, changes "checkbox" in code to "CheckBox" and tried your code as custom calculation of my text field but somehow it is not working. Am I doing something wrong?

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
LEGEND ,
Sep 18, 2015 Sep 18, 2015

You can look at the JavaScript console (Ctrl+J) to see of any errors are reported. Otherwise, I'd have to look at the form.

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 ,
Sep 18, 2015 Sep 18, 2015

sent you a link to google drive with the file!

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
LEGEND ,
Sep 18, 2015 Sep 18, 2015

There is an error reported in the JavaScript console. You form has four check boxes, named: "CheckBox", "CheckBox1", "CheckBox2", and "SUBMIT". The for loop start like this:

    for (i = 1; i < 4; i += 1) {

This would work if there were a check box named "CheckBox3", but since there isn't, the script stops with the following reported in the console:

TypeError: getField("CheckBox " + i) is null

17:Field:Calculate

This means that the field it's trying to access (CheckBox3) doesn't exist. So you need to either add the additional check box or reduce the number of iterations of the for loop (change 4 to 3).

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