Skip to main content
New Participant
September 17, 2019
Question

How can I populate a text field from list box selections

  • September 17, 2019
  • 2 replies
  • 2057 views

I need to populate a text box with multiple selections from a list box of 250 countries. I don't know if this is possible or if there would be a better way to do it. If there is an easier way to choose the countries other than a list box, I would love some input on that. My work has taken me in a lot of different directions in the last couple of weeks and I think my brain is overloaded with too much new info to be able to figure this out where it makes sense. HELP!

    This topic has been closed for replies.

    2 replies

    NagannudjAuthor
    New Participant
    September 26, 2019

    I think Mr. Kremer's answer is the major part of what I needed but I'm still not clear on how to get the list box selections to go into the text box correctly. I haven't ever needed a list box before so this is a first for me. I'm not sure that I'm setting it up right to begin with because the selections aren't going into the text box immediately. I've assumed that since this is a custom calulation script, the export value for each selection should be a number from 1 to 250. Is that correct? Once the selections are made, they should automatically go into the text box and the text box should be read only. Something's missing that would make that happen.

    NOTE:  I'm using Acrobat Pro 10 and cannot upgrade yet for too many reasons to go into.

     

    Besides the list box and the text box, I have a YES checkbox and a NO checkbox. If YES is checked, the list box of 250 countries should appear. If NO is checked, the list box should disappear if the user checked YES by mistake and also make the text box be grayed out and read only so that the user moves on to the next field automatically.

     

    So ––

    • I nee the 2 checkboxes that trigger or untrigger the list box

    • I need the listbox with multiple selections checked that appears if YES is checked

    • I need the text box to fill automatically with the selections from the list box

     

    Lastly -- do I need to use the selection change tab and put a script in there to make what I need happen? I'm sure I sound like an idiot but I've been away doing this for awhile so apparently, I am indeed an idiot!

     

     

    Karl Heinz  Kremer
    Community Expert
    September 17, 2019

    When you look at the listbox API documentaiton, you will see that it is possible to get all the selected items:

     

    https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FField_properties.htm%23TOC_currentValueIndicesbc-13&rhtocid=_6_1_8_31_1_12

     

    So one approach would be to do something like this as the field calculation script:

     

     

    var f = this.getField("List Box1");
    var v = ""; 
    // get all selected items from the list box - this is based on the example in the API documentation
    
    var a = f.currentValueIndices;
    if (typeof a == "number")     // A single selection
       v = f.getItemAt(a, false);
    else {     
        // Multiple selections
        for (var i = 0; i < a.length; i++) {
            v += f.getItemAt(a[i], false);
            if (i != a.length-1) {
                v += ", ";
            }
        }
    }
    
    event.value = v;

     

     

    This is based on the example you will find in the API documentation.