views:

141

answers:

4

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?

A: 

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.

jball
Rofl. Good point. So scratch that concern. Would my stated approach be the best way of going about it?
Matrym
Some of the other answers have examples of using the array's `join` method with `\n` as the delimiter. That would be a good way to go.
jball
+3  A: 

use the array's join() method:

["a", "b", "c"].join("\n")
emh
+2  A: 
myarray.join("\n")

put that as the textarea value

Larry K
+1  A: 

Try with join

text = array.join("");
document.write(text); // or what ever you would do with text 
streetparade
Won't be on separate lines per SO request
Larry K