Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: get lines

  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool get lines

    Hello every body

    i have a problem with javascript

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

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

    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

    i am so sorry about the weak language

    thank you in advance

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    not sure that I really understand the question, but maybe this is a start:

    Code:
    <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>

  • Users who have thanked xelawho for this post:

    yassor (09-27-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    What do you mean by "process"???

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

    But briefly:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    yassor (09-27-2011)

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    @Old Pedant: Sloppy, why make line a global?

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

    Code:
    <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>
    Last edited by ironboy; 09-27-2011 at 12:57 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •