4 Replies Latest reply on Oct 11, 2008 6:25 AM by (chung_lee)

    Populate a text box with a listbox?

      Acroforms - Javascript - Adobe Acrobat Pro v8.12<br /><br />Hello all...<br /><br />How can I use Acrobat javascript to populate a text box with <br />the multi selected values from a list box?<br /><br />EXAMPLE:<br /><br />---------------------<br />   MultiSelect<br />    Listbox1<br /><br /><Blank><br />Apples<br />[Oranges]  <--Selected<br />Pears<br />Cherries<br />[Grapes]   <--Selected<br />Figs<br />[Plums]    <--Selected<br />---------------------<br /><br />RESULTS:<br />---------------------<br />    Textbox1<br /><br />Selected: Oranges, Grapes, Plums<br />---------------------<br /><br />I placed the following Keyboard Script in<br />Combo box2, to populate Textbox2 with the<br />selection from Combo box2:<br /><br />var targ1=getField("Textbox2");<br />if(!event.willCommit){<br />    targ1.value=event.change;<br />}<br /><br />Can it be modified to handle the multi selected<br />items in listbox1 to populate Textbox1?
        • 1. Re: Populate a text box with a listbox?
          MarkWalsh Level 4
          This is something I was using with a series of check boxes. You should be able to adapt it to use the values in listbox1 (change the middle section starting with var a = oField.getArray). Enter the script as a custom Calculation for your textbox.

          var fieldValue
          var newFieldValue
          var delimiter

          newFieldValue = ""
          delimiter = ", "

          var oField = this.getField("listbox1");
          if (oField != null) {

          //Note - Change this section to use your listbox values;
          var a = oField.getArray();
          for (j =0; j < a.length; j++) {
          var FieldName = a[j].name;
          if (this.getField(FieldName) != null) {
          fieldValue = this.getField(FieldName).valueAsString;
          if (fieldValue != "Off" ) {
          newFieldValue = newFieldValue + fieldValue + delimiter;
          }
          }
          }
          // End of section;

          }
          newFieldValue = newFieldValue.substring(0, newFieldValue.length - delimiter.length)
          event.value = newFieldValue
          • 2. Re: Populate a text box with a listbox?
            Level 1
            Thank for the quick response.

            Will give it a try and let you know.
            • 3. Re: Populate a text box with a listbox?
              gkaiseril MVP & Adobe Community Professional
              Multiple selection is available only for list boxes, you have to select the option on the field's ' Option' tab. On the "Selection Change" tab for the "This script executes when the listbox's selection changes:" :

              if (event.willCommit) {
              var a = event.target.currentValueIndices;
              if (typeof a == "number") // for single selection
              this.getField("Textbox1").value = event.target.getItemAt(a, false);
              else { // multiple selection
              for (var i = 0; i < a.length; i ++)
              if (i == 0) // for first item in selection
              this.getField("Textbox1").value = event.target.getItemAt(a, false);
              else // for 2nd and on selection
              this.getField("Textbox1").value += ", " + f.getItemAt(a[i], false);
              } // end for
              } // end willCommit

              More information is in the Acrobat JavaScript reference.
              • 4. Re: Populate a text box with a listbox?
                Level 1
                Thank you Mark and Geo for your efforts. Tried both scripts. Mark: your script works with a single selection from the list box, but multiple selections generate a blank in the text box. Geo: a single selection from the list box and the text box is populated with the previously selected value. Multiple selections generate an error. Any guidance you can provide this newbie is greatly appreciated.