I have a form and the submit action will be to javascript:funtion(); and I want to take make an array out of the value of a textarea where each new variable in the array is from a new line (like the values in the textarea separated by line breaks). Is it possible?
@toicontienFeb 19.2008 — #The funny thing is the rn line characters [I]only[/I] exist when working directly with the TEXTAREA's .value property. If you assign the TEXTAREA's value to another variable, Opera seems to use only n characters to denote newlines. To be safe, pass the TEXTAREA's value to the String function to force-convert it to an actual String variable.
// txtArea is a variable with a node reference to a TEXTAREA var val = String(txtArea.value);
// Now all rn characters in Opera should be converted to n chars var lines = val.split("n");
@KorFeb 19.2008 — #Looks like Opera is confused between the n [I]newline[/I] and the r [I]carriage return[/I]. That is to be studied... Thanks, [B]toicontien[/B] ?
@toicontienFeb 19.2008 — #No problem. And post back what you find. I'm making a JavaScript code editor right now to enhance normal TEXTAREA's, and let me tell you. What a pain it's turning out to be.
I almost think that when a user is entering text, pressing ENTER inserts a carriage return and a newline, yet new line characters in everything else only require a newline character. Yeesh.
@Arty_EffemFeb 19.2008 — #Note that the Opera browser on Windows uses rn characters to denote newlines, not just n. You might want to use this regex:
var txtArray = v.match(/rn|n/g);
[/QUOTE]
To split on either format, I would use: [CODE]var arr=textarea.value.split(/r*nr*/));[/CODE] The test below suggests that I.E. and Opera both use rn, while Mozilla and Safari(Win) use only n. Note the way the array is laid out in the different alerts.
nr is detectable only with Opera and only when the textarea contains an empty line, but this may be due to the way different regex engines interpret these pairs.
[CODE]<html> <head> <title>Handling nr carriage return characters</title> </head> <body> <form> <textarea cols=80 rows=6 name=ta1> This is a textarea containing lines that end with hard returns.</textarea><br> <input type=button value="Split on /r*nr*/" onclick="alert( (this.arr=ta1.value.split(/r*nr*/))+'nnLength:'+this.arr.length )"> <input type=button value="Split on /r/" onclick="alert( (this.arr=ta1.value.split(/r/))+'nnLength:'+this.arr.length )"> <input type=button value="Split on /n/" onclick="alert( (this.arr=ta1.value.split(/n/))+'nnLength:'+this.arr.length )">
@Arty_EffemFeb 20.2008 — #Interesting is that my code (#2) with a simple n split, works OK on all the browsers I have: FF2, IE7, IE6, Opera9 / XP-SP2
So: why using that RegExp tests and carriage return r ? Did I miss something? Are there any other browsers which need that?[/QUOTE]If you split on n in I.E. and Opera, the string will contain one character more. Unlikely to be a problem.
@KorFeb 20.2008 — #If you split on n in I.E. and Opera, the string will contain one character more. Unlikely to be a problem.
http://veryuseful.info/arrayfromta/[/QUOTE] Oh, I see. Each element of the array will have an extra empty space at the end in IE and Opera... It can be removed easely.