1 Reply Latest reply on Apr 5, 2011 11:51 PM by George_Johnson

    How to display multiple responses from a list box in a text field, appended format

    rhunter100

      I'd like to take the selected responses from a (multiple selection) listbox and display them in a text field in an appended (comma, space) format.

       

      For example: Depression List (txtField): loss of appetite, loss of energy, unable to concentrate, thoughts of suicide (and so on).  

       

      Thanks for your help.

        • 1. Re: How to display multiple responses from a list box in a text field, appended format
          George_Johnson MVP & Adobe Community Professional

          The value of a list box will either be a string (if none or one item is selected) or an array (if more than one item is selected). So your code has to determine which it is, and if an array (object), loop through it and get the text of each selected item and add the comma and space. If it's not an array, just use the returned value. Here's a simple custom Calculate script that you can use in the text field:

           

          // Custom Calculate script for text field
          (function () {
          
              // Initialize the string
              var s = "";
          
              // Get the value of the list box (string or array)
              var v = getField("your_listbox").value;
          
              // If more than one item is selected, the type of the value will be "object" (an array really)
              if (typeof v === "object") {
          
                  // Loop through the array to build up a string
                  for (var i = 0; i < v.length; i++) {
                      if (s) s += ", ";  // Add a comma and a space if needed
                      s += v[i];  // Add the selected value
                  }
          
                  // Set this field value to the string
                  event.value = s;
          
              } else {
          
                  // Set this field value to the one item selected
                  event.value = v;
              }
          
          })();
          

           

          Replace "your_listbox" with the actual name of the listbox you're using.

          1 person found this helpful