Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting a string

Status
Not open for further replies.

qwe1204

Programmer
Aug 30, 2001
21
GB
I have a textarea on my .asp page. It contains upto 17 lines of varying length text with a carriage return line feed on the end of each line. I need to split this string into 17 separate string using javascript and store these new strings in hidden variables on the page.
 
<form name=&quot;frmX&quot;>
<TEXTAREA rows=5 cols=40 id=textarea1 name=textarea1>
</TEXTAREA>
<INPUT type=&quot;button&quot; value=&quot;Button&quot; id=button3 name=button3 onClick=&quot;splitIt(textarea1.value);&quot;>
</form>

Javascript function:

function splitIt(txtArea)
{
var txtArray = txtArea;
txtArray.split(Chr(13) & Chr(10));
}

Now if I did the function correctly, you should end up with an array (txtArray) that contains the strings you want.
 
Skip what I have above, it doesn't work. Try this (I tested it):

Javascript function:
function splitIt(txtArea)
{
var txtArray = txtArea;
var re = /\r\n/g;

hold = txtArray.split(re);
holdL = hold.length;

for(x=0;x<holdL;x++)
alert(hold[x]);
}

<form name=&quot;frmX&quot;>
<TEXTAREA rows=5 cols=40 id=textarea1 name=textarea1>
</TEXTAREA>
<INPUT type=&quot;button&quot; value=&quot;Button&quot; id=button3 name=button3 onClick=&quot;splitIt(textarea1.value);&quot;>
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top