Skip to main content
Participating Frequently
July 23, 2019
Answered

How to ? Checkboxes to fill Textbox in a specific way

  • July 23, 2019
  • 1 reply
  • 3387 views

Hello hello everyone,

(First of all, I'm truly sorry for my poor english and thus, for this question badly written, I'm not an english speaker)

(Second, I already tried to find an answer by searching through the forums but I didn't find anything that was of any help with my pretty poor abilities to understand Javascript (and I already asked my question in the french version, which didn't give any result))

Here is the issue I'm facing :

A series of checkboxes whose number associated (I suppose the value?) must be transferred in a text area when checked. Problem “bis”, I don’t understand javascript and the many examples I’ve found on the forum about similar issues, hence I can’t ‘translate” them to my particular situation.

More precise description:

I have a series of checkboxes (in an interactive PDF form that I make with ADOBE ACROBAT PRO), each one represents a certain number "1", "2", "2024", etc. => in fact a code in a list of work risks. The risk "Fall" has the code "1", the "chemical exposure" risk has the risk code "2015", the "screw drilling" risk "14554", etc. etc.

There are about fifteen boxes, each one linked to a risk, therefore to a code.

People can tick these boxes, to say what risks they are exposed to, but I also would like, and this is my concern, to ensure that once the boxes are ticked, the numerical code associated with them is returned in a text box (e.g. Text1), one after the other, in the form of "code#1, code#2, code#3, etc. ».  (So comma+space "code" comma+space "code", etc.)

I know how to create boxes / text field. What I can't do is create the javascript script that makes sure that if I check one box, it goes into the text, that if I check two, no matter which ones, their associated codes go into the text box following each other, and that if I uncheck one check, then its associated code disappears from the text box, without making the data in the other checked boxes disappear.

Intial situation :

Nothing is checkeck ( o ) => Nothing in the textbox

  1. Risk 1                    Code 1                  o
  2. Risk 2                    Code 2                  o
  3. Risk 3                    Code 254              o
  4. Risk 4                    Code 7568            o

Texte :

Situation 1 :

3 boxes are checked (X) => The textbox is automatically field with the code associated to the boxes

  1. Risk 1                    Code 1                  o
  2. Risk 2                    Code 2                  x
  3. Risk 3                    Code 254              x
  4. Risk 4                    Code 7568            x

2, 254, 7568

Te

Sixte :

Situation 1 (bis) :

2 boxes are checked, one was 'un'checked => The textbox automaticaly adapt itself

  1. Risk 1                    Code 1                  o
  2. Risk 2                    Code 2                  x
  3. Risk 3                    Code 254              o
  4. Risk 4                    Code 7568            x

2, 7568

Te

Thank you very much for reading so far.

Olivier

This topic has been closed for replies.
Correct answer try67

You can email it to me directly via [try6767 at gmail.com] or you can upload it to a file-sharing website (Google Drive, Dropbox, Adobe DC, etc.) and post the link to it here.


I'm noticing now there are errors in the code Bernd provided before. You can use this code instead:

if (this.getField("Checkbox1").valueAsString != "Off") event.value = "1";

else event.value = "";

if (this.getField("Checkbox2").valueAsString != "Off") {

  if (event.value != "") event.value += ", ";

  event.value += "2";

}

However, since you have a lot of check-boxes I would suggest doing it differently.

As the export value of each check-box enter the code for that box, and then you could use this more generic script as the custom calculation script of your text field:

var fields = ["Checkbox1", "Checkbox2"]; // etc.

var selectedCodes = [];

for (var i in fields) {

    var f = this.getField(fields);

    var v = f.valueAsString;

    if (v!="Off") selectedCodes.push(v);

}

event.value = selectedCodes.join(", ");

All you have to do is enter the full list of field names in the first line.

1 reply

Bernd Alheit
Community Expert
Community Expert
July 23, 2019

Use a calculation script for the textbox.

Test the checkboxes and add the codes to event.value

Participating Frequently
July 23, 2019

Thank you for your very quick answer but it doesn't help me.

I do already understand that I need to add script to my text box and that I have to modify something in my checkbox parameter, but after that, as I said I'm a bit lost.

My skills aren't enough to "write" the code by myself. I don't even grasp the meaning of " event.value" you're referring to. That's why I find it hard to apply the similar solution I've already found on this forum.

Could you give me an example where I could clearly see what part are to be modified by my own data and what part are the code to copypaste?

Knowing that my text field is called "Text1" and that the Checkboxes are all "Checkbox1", "CheckBox2", etc.

try67
Community Expert
Community Expert
July 23, 2019

I place the script into the validation panel of my "text1". When I make the JS Console appear and that I thick on the boxes, nothing happened, anywhere, not in the PDF, not in the console.

Moreover, when I write something in the textbox (eg : 1) it automaticaly filled another ", 2" juste right after in the box and become unmodifiable, undeletable.


Please read the instructions carefully. This is a ​calculation​ script, not a validation one.