/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Make an array from a textarea?

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?

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 19.2008 — You may split ([B]split()[/B] method) the textarea value upon the javascript [i]newline[/i] special character ([B]n[/B])
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function makeAray(v){
var txtArray=v.split('n');
for(var i=0;i&lt;txtArray.length;i++){
alert(txtArray[i]);
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;textarea name="txt"&gt;&lt;/textarea&gt;
&lt;input type="button" value="Make Array" onclick="makeAray(txt.value)"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@toicontienFeb 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);

I [I]think[/I] that will cause the regex engine to first try matching the rn characters, and then if that fails match the n character.
Copy linkTweet thisAlerts:
@KorFeb 19.2008 — I have not installed Opera on the PC I'm on it right now, but I will test it tomorrow.
Copy linkTweet thisAlerts:
@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");
Copy linkTweet thisAlerts:
@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] ?
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@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 )">

<input type=button value="Detect n" onclick="alert( /n/.test(ta1.value) )">
<input type=button value="Detect r" onclick="alert( /r/.test(ta1.value) )">
<input type=button value="Detect nr" onclick="alert( /nr/m.test(ta1.value) )">
<input type=button value="Detect rn" onclick="alert( /nr/m.test(ta1.value) )">

</form>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@KorFeb 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?
Copy linkTweet thisAlerts:
@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.

http://veryuseful.info/arrayfromta/
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@KorFeb 20.2008 — Fixed:
<i>
</i>function makeAray(v){
var txtArray=v.split('n');
for(var i=0;i&lt;txtArray.length;i++){
/s$/.test(txtArray[i])?txtArray[i]=txtArray[i].replace(/s$/,''):null;
alert(txtArray[i]+' has the length '+txtArray[i].length)
}
}
Copy linkTweet thisAlerts:
@jacjil2authorFeb 20.2008 — Wow! You guys are awesome! Thank you very very much!?
×

Success!

Help @jacjil2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...