Matrym
Matrym

Reputation: 17053

Javascript: Put array into a text field

How would you go about putting an array into a textarea, each value on its own line? I thought about converting the array to a string, and the commas into new lines, but what if the value of an array item has a comma?

Upvotes: 0

Views: 1697

Answers (4)

streetparade
streetparade

Reputation: 32888

Try with join

text = array.join("");
document.write(text); // or what ever you would do with text 

Upvotes: 1

Larry K
Larry K

Reputation: 49104

myarray.join("\n")

put that as the textarea value

Upvotes: 3

emh
emh

Reputation: 1299

use the array's join() method:

["a", "b", "c"].join("\n")

Upvotes: 4

jball
jball

Reputation: 25014

If you have a comma delimited array that contains values with unescaped commas, you have an insurmountable problem. You need to convert the array into a string with some other sort of delimiter, preferably the linebreaks you want them to be in the end.

Upvotes: 0

Related Questions