470,647 Members | 1,268 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Textarea and javascript

Is it possible to read each line from a HTML textarea into a
javascript array. For example:
If I were to input into the textarea:
1
4
6
3
Could I somehow have javascript read it so:
array[1]=1
array[2]=4
array[3]=6
array[4]=3

And also, is it possible to find how many lines long a textarea is?

Thanks for the help, it is greatly appreciated.
Jul 20 '05 #1
1 12093
Lee
CiscoGuy said:

Is it possible to read each line from a HTML textarea into a
javascript array. For example:
If I were to input into the textarea:
1
4
6
3
Could I somehow have javascript read it so:
array[1]=1
array[2]=4
array[3]=6
array[4]=3

And also, is it possible to find how many lines long a textarea is?

Thanks for the help, it is greatly appreciated.


Note that the first element of an array is indexed as 0,
not 1. Otherwise, this seems to be generally what you want:

<html>
<head>
<script type="text/javascript">
function textareaToArray(t){
return t.value.split(/[\n\r]+/);
}
function showArray(a){
var msg="";
for(var i=0;i<a.length;i++){
msg+=i+": "+a[i]+"\n";
}
alert(msg);
}
</script>
</head>
<body>
<form>
<textarea rows="10" cols="20" name="alpha"></textarea>
<br>
<input type="button"
value="show array"
onclick="showArray(textareaToArray(this.form.alpha ))">
</form>
</body>
</html>

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Simon | last post: by
4 posts views Thread by Csaba Gabor | last post: by
5 posts views Thread by lawrence | last post: by
4 posts views Thread by TJS | last post: by
5 posts views Thread by bwucke | last post: by
6 posts views Thread by lamp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.