...

View Full Version : get lines



yassor
09-27-2011, 01:14 AM
Hello every body :thumbsup:

i have a problem with javascript

i wrote a script which process the entry of a text area 1 :D

and i made the script process only the first line of the entry :thumbsup:

i want a method to make the script to process all of the lines which the user enter it

OR make a varibals to every line entered from the user :p

i am so sorry about the weak language :D

thank you in advance :rolleyes::rolleyes:

xelawho
09-27-2011, 01:52 AM
not sure that I really understand the question, but maybe this is a start:



<textarea id="txt" rows="5" cols="20" ></textarea>
<input type="button" value="Show lines" onclick="splitLines()">
<div id="mydiv"></div>

<script type="text/javascript">
function splitLines() {
lines=document.getElementById("txt").value.split(/\n|\r/g);
for (var k=0; k<lines.length; k++) {
document.getElementById("mydiv").innerHTML+=lines[k]+'<br>';
}
}
</script>

Old Pedant
09-27-2011, 02:16 AM
What do you mean by "process"???

What do you want to *DO* to the lines?

But briefly:


<script>
function processTextArea( ta )
{
var lines = ta.value.split("\n");
for ( var i = 0; i < lines.length; ++i )
{
line = lines[i];
... do something with line ...
}
}
</script>
..
<form>
<textarea name="something"></textarea>
<input type="button" value="Click to process textarea" onclick="processTextArea(this.form.something);"/>
...
</form>


***********

EDIT: Teach me not to hit REFRESH.

ironboy
09-27-2011, 12:49 PM
@Old Pedant: Sloppy, why make line a global? :cool:

Otherwise Old Pedants code is perfectly fine, a small syntactic alternative would be


<script>
function processTextArea( ta ){
var line, lines = ta.value.split("\n");
while((line = lines.shift()) || line!==undefined){
// do something with line
}
}
</script>
..
<form>
<textarea name="something"></textarea>
<input type="button" value="Click to process textarea" onclick="processTextArea(this.form.something);"/>
...
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum