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

Need Help Creating Fillable Form - Using Javascript with List Box and Multiple Selections

New Here ,
Oct 20, 2021 Oct 20, 2021

I need help with a form I am creating. I have a "List of Choices" box on my form that has all the Florida Cities. What I am trying to do is, create a button that moves their selection (or selections) to another box. What is the JavaScript for this action? Also, I want to be able to clear the box when the "Clear Form" button is pressed. Is this possible?

Example.pngexpand image

 

TOPICS
How to , JavaScript
1.3K
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
Community Expert ,
Oct 20, 2021 Oct 20, 2021

Hi,

 

gassumed the listbox is names "ListBox" and the text field "TextField" this should do the trick:

var oList = this.getField("ListBox"),
    oField = this.getField("TextField"),
    aSelection = oList.currentValueIndices,
    cOutput = "";
if (typeof aSelection == "number") {
    cOutput += "\n" + oList.getItemAt(aSelection, false);
} else {
    for (var i = 0; i < aSelection.length; i ++) {
        cOutput += "\n" + oList.getItemAt(aSelection[i], false);
    }
}
oField.value = cOutput;

 

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
Community Expert ,
Oct 20, 2021 Oct 20, 2021

Hi,

 

gassumed the listbox is names "ListBox" and the text field "TextField" this should do the trick:

var oList = this.getField("ListBox"),
    oField = this.getField("TextField"),
    aSelection = oList.currentValueIndices,
    cOutput = "";
if (typeof aSelection == "number") {
    cOutput += "\n" + oList.getItemAt(aSelection, false);
} else {
    for (var i = 0; i < aSelection.length; i ++) {
        cOutput += "\n" + oList.getItemAt(aSelection[i], false);
    }
}
oField.value = cOutput;

 

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 ,
Oct 21, 2021 Oct 21, 2021

I placed the JavaScript into the "Text Box" under the actions tab, and if you press the "Add" button it does move it from the "List Box" to the "Text Box". However, if you select multiple cities and press "Add"  they appear all crammed together in the "Text Box". Is there a way I can fix this?

Screenshot 2021-10-21 151418.pngexpand image

 

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 Expert ,
Oct 21, 2021 Oct 21, 2021

Did you set the text field as Multiline?

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 ,
Oct 22, 2021 Oct 22, 2021

Yes

Screenshot 2021-10-22 105405.pngexpand image

 

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 ,
Oct 22, 2021 Oct 22, 2021

Nevermind, I found the problem and it's now working PERFECTLY! Thank you so much for your help, I've been searching for over a week for a way to do this. 

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 ,
Aug 02, 2022 Aug 02, 2022

How do you remove the space at the top of  List Box2? I've tried all the formatting tricks I know, but nothing fixes the problem. Even if you only make one selection and hit the "add" button it still puts an empty space at the top. Can this be fixed?

Shanda25495492ozxu_0-1659476103713.pngexpand image

 

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 Expert ,
Aug 02, 2022 Aug 02, 2022

How are you populating it? If using a script, post the code, please.

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 ,
Aug 03, 2022 Aug 03, 2022

This is the code I am using. I put the code under the action tab for the button.

 

var oList = this.getField("List Box1"),
oField = this.getField("List Box2"),
aSelection = oList.currentValueIndices,
cOutput = "";
if (typeof aSelection == "number") {
cOutput += "\n" + oList.getItemAt(aSelection, false);
} else {
for (var i = 0; i < aSelection.length; i ++) {
cOutput += "\n" + oList.getItemAt(aSelection[i], false);
}
}
oField.value = cOutput;

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 Expert ,
Aug 03, 2022 Aug 03, 2022

In these lines move the "\n" part to the end:

cOutput += "\n" + oList.getItemAt(aSelection, false);

cOutput += "\n" + oList.getItemAt(aSelection[i], false);

So like this:

cOutput += oList.getItemAt(aSelection, false) + "\n";

cOutput += oList.getItemAt(aSelection[i], false) + "\n";

 

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 ,
Aug 04, 2022 Aug 04, 2022
LATEST

Thank you!

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 Expert ,
Oct 20, 2021 Oct 20, 2021

You can access the field's value using the value property. Note it will return an Array if multiple items are selected, or a String if only one item. You can then use the insertItemAt method to add new items to the other field. You might want to make sure those items aren't already there, though.

To clear the list of items you can use the clearItems 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