Textarea and javascript

C

CiscoGuy

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.
 
L

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+"\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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top