Ryan Ryan - 4 years ago 64
Javascript Question

Explode string and make a line break after each word

I have a textbox (I usually call it like this:

document.forms[0].text.value
)
that has this kind of value:

a,b,c,d,e,f,g,etc


what I want to do is "explode" (like the php function) on each of the commas in the above string, then put it back into the textbox so I end up with this:

a
b
c
d
e
f
g
etc


Doing a little Googling I see I will need to use
split()
but doing something like:

st.split(",") + "<br />";


is giving me nothing but invalid results.

Answer Source

Either:

st.split(",").join("\n");

or:

st.replace(/,/g,"\n");

Since you're putting it in a textarea, by the look of things, you need newlines, not BR tags.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download