Forum Moderators: open

Message Too Old, No Replies

Read and combine contents of two textareas

then display results it in a third textarea

         

dmann

12:13 am on Dec 10, 2009 (gmt 0)

10+ Year Member



Hello.

I'm very new to javascript and cannot figure out how to do the following.

I want to have two textareas in a form for input, TA1 and TA2.

Each will contain a list of words on a separate line. The number of words in each list can vary and need not be the same.

When the user clicks on a button, I want to read the contents of each textarea, then combine and display them in a third text area, TA3, as in this example.

User inputs in TA1:

one
two
three

User inputs in TA2:

beer
wine

Output in TA3 would be: (whichever is easier)

one beer
two beer
three beer
one wine
two wine
three wine

OR

one beer
two beer
three beer
one wine
two wine
three wine

It seems as if it should be easy enough, but I've not made any headway other than reading the contents of either TA1 or TA2 and displaying the exact contents in TA3.

Thank you in advance for any help.

Expo

6:58 am on Dec 10, 2009 (gmt 0)

10+ Year Member



Text area isn't the way to do it, they're not designed for the output you want.

The problem here is that if a user inputs one two three into a text area the contents will read back as a string "one two three" so appending one to the other would give you "one two three beer wine".

also the two output examples you posted are the same.

It's difficult to suggest without really knowing what you're trying to do but you should take a look at other form elements such as combo box and textfields.

caribguy

7:16 am on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Each line in your textarea fields is separated by '\n'

Build an array for the contents of TA1 and TA2, something like:

ta1_content = myform.ta1_area.value.split('\n')
ta2_content = myform.ta2_area.value.split('\n')

You can limit the length of the array by passing a second argument to the split() method, i.e my_string.split('\n',4)

Then use two nested loops to iterate through your arrays
and add the current values: ta3_string += ta1_content[i] + ' ' + ta_content[j] + '\n'

return the end result to your 3rd textarea:

myform.ta3_area.value = ta3_string

dmann

8:00 pm on Dec 10, 2009 (gmt 0)

10+ Year Member



Thank you both for the replies.

I ended up combining both your suggestions. I used two sets of text boxes to limit the number of words and read the content of each set into an array. Then it was a simple matter of using two nested loops to combine them.

Expo, the second set above was supposed to be

one beer
one wine
two beer
two wine
three beer
three wine

I really have to stop doing this stuff late at night. ;-)

Thanks again!