Expand my Community achievements bar.

Split Text into multiple fields/auto populate multiple fields

Avatar

Former Community Member

Basically, I would like the text in one text box to be split up to auto-populate other textboxs in my form, each peice of info is separated by a "-".

Example:

Main Text Box: "Data A - Data B - Data C - Data D"

Question 1: Data A

Question 2: Data B

Question 3: Data C

Question 4: Data D

Basically I want the user to type/paste something into my main text box, and the data (which is separated by a "-") to be split up and auto filled into the corresponsding text fields below. Thanks a bunch

9 Replies

Avatar

Level 10

Hi,

this can be done via JavaScript.

Here an Example.

var Input = Textfield1.rawValue;

var TextParts = Input.split("\u0020");

// If there is one word, populate Textfield1

if (TextParts.length === 1)

{

Textfield2.rawValue = TextParts.slice(0,1).toString();

Textfield3.rawValue = "";

Textfield4.rawValue = "";

}

// If there are two word populate Textfield1 & Textfield3

if (TextParts.length === 2)

{

Textfield2.rawValue = TextParts.slice(0,1).toString();

Textfield3.rawValue = "";

Textfield4.rawValue = TextParts.slice(1,2).toString();

}

// Else populate all three Textfields

if (TextParts.length === 3)

{

Textfield2.rawValue = TextParts.slice(0,1).toString();

Textfield3.rawValue = TextParts.slice(1,2).toString();

Textfield4.rawValue = TextParts.slice(2,3).toString();

}

Avatar

Level 3

I am looking to do something very similar.

I have a drop-down and need to split the Item Values, which have 3 choices separated by commas, to populate 3 different text fields. This is where I am having problems. Can't figure out how to access the values. I think it is close to what is here just modified somewhat. Any help will be greatly appreciated!

Thank You, Gary

Avatar

Level 10

Hi Gary,

Using JavaScript you can use the split method to access the values separated by commas, so in the change event of the drop down list add the following code;

var valuesArray = xfa.event.change.split(",");

TextField1.rawValue = valuesArray[0];

TextField2.rawValue = valuesArray[1];

TextField3.rawValue = valuesArray[2];

You will have to change TextField1, TextField2, and TextField3 to match your field names

Hope this helps

Bruce

Avatar

Level 3

Hi Bruce, thanks but not quite. Sorry, guess I wasn't clear enough. I need to have a list of single choices in the drop-down, and pull three different values from the comma separated values in the binding tab, not from the text in the drop-down.

Avatar

Level 10

Hi Gary,

So are you wanting to make multiple selections from the one drop down list?  The drop down list does not support this, you will have to use a listbox or maybe a set of checkboxes.

Regards

Bruce

Avatar

Level 3

Hi Bruce, and thanks for your time.

No, my drop-down list consists of single choices, not choices that are comma separated. I want the item values of these to be comma separated. Then I want the comma separated item values (3), so populate three different text fields. Just like the sample you gave me, except I need to use the binding tab item values to populate the text fields. I am still pretty new to javascript, so forgive my lack of knowledge.

Drop-down would look like this:

A

B

C

With item values being (these are what I need to populate the text fields)

A = 1,2,3

B = 4,5,6

C = 7,8,9

Not A,B,C as the drop-down choice.

So when A is chosen, it populate text field 1 with 1, text field 2 with 2, and text field 3 with 3.

So when B is chosen, it populate text field 1 with 4, text field 2 with 5, and text field 3 with 6.

So when C is chosen, it populate text field 1 with 7, text field 2 with 8, and text field 3 with 9.

Avatar

Level 10

OK,  I think I am getting there.  You can use the boundItem method of the dropdown list control to look up an item value given a display value, so in the change event of the dropdown list using JavaScript use;

var boundValue = this.boundItem(xfa.event.change);

var valuesArray = boundValue.split(",");

TextField1.rawValue = valuesArray[0];

TextField2.rawValue = valuesArray[1];

TextField3.rawValue = valuesArray[2];

Note: This will only work if all the display items are unique, if you were to have a fourth item " A = '10,11,12' " then you will only ever get the first "A" back.

I hope this is what you are after.

Regards

Bruce

Avatar

Level 3

Thanks Bruce, that is what I was looking for! I appreciate your time and patience.

And in reference to your note, I noticed that it not only returns the first instance of the drop-down choice, but also the first instance in the bound values as well. From your statement, I am guessing there is no way around this? For the bound values I mean. Won't typically be an issue, but interested in knowing if I did need it if there was a way to fix that?

Thanks again.

Avatar

Level 10

Hi,  I don't think there is a way around handling duplicate items, I would have thought xfa.event.change would be the value of the drop down not the display text or maybe even a index within the list, but then I guess they thought there would not be duplicate display text in a drop down (the listbox has the same behaviour).

You can loop though the items using the following code, so if you could find the index number then maybe you could come up with some sort of work around.

var dditems = DropDownList1.resolveNodes("#items.(save=='1').#text[*]");

for (var i = 0; i < dditems.length; i++)

{

    console.println(dditems.item(i).value);

}

To loop around the display items change save == '1' to save == '0'.

There is also the getSaveItem and getDisplayItem methods which allow you to go directly to the item if you new the index.

Good luck

Bruce