MortenMoulder
MortenMoulder

Reputation: 6646

Update textarea with value of multiple input elements

This is a bit more complicated than the title says. I have 10 input elements the values of which I would like to copy to my textarea. Input field 1 is line 1, input field 2 is line 2, and so on.

I want to automatically fill out the textarea when the input fields are filled out. Text wrapping doesn't matter, so if input field 1's text is long, it may stretch to line 2 (because of text wrapping), but input field 2 should always be on a new line after input field 2. Get my point?

All of the input fields have same class, so it's easy (I guess). I just cannot wrap my head around it. I guess you can map it, and do something like this, but I'm not sure:

<input type="text" name="text[]" />

Upvotes: 0

Views: 730

Answers (1)

twinlakes
twinlakes

Reputation: 10238

You said they all have the same class, so I'll call it "myClass".

var texts = [];
$('input.myClass').each(function () {
  texts.push($(this).val());
});
$('textarea').val(texts.join('\n'));

Upvotes: 2

Related Questions