How to Count the number of lines?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anz

    #1

    How to Count the number of lines?

    Is there any javascript function for counting the number of lines
    inside a text area ?
  • RoLo

    #2
    Re: How to Count the number of lines?

    On Jul 1, 3:39 am, Anz <anzbmuham...@g mail.comwrote:
    Is there any javascript function for counting the number of lines
    inside a text area ?
    try:
    a.value.split(" \n").length

    Comment

    • Bart Van der Donck

      #3
      Re: How to Count the number of lines?

      RoLo wrote:
      On Jul 1, 3:39 am, Anz <anzbmuham...@g mail.comwrote:
      >
      >Is there any javascript function for counting the number of lines
      >inside a text area ?
      >
      try:
      a.value.split(" \n").length
      I'm afraid it's not that simple. The number of lines in a textarea
      does not necessarily represent the number of newline characters. You
      are right that your solution should work for a <textarea wrap="off">
      in MSIE and FireFox, although that will not qualify as valid X/HTML.

      The original poster should first be clear whether or not the text in
      his <textareashou ld wrap.

      1) If yes:
      Read out the number of columns with [obj].cols as to split the content
      accordingly, but only if the wrapped string must be considered as
      multiple lines. In the other case, use a.value.split(" \n").length.

      2) If no:
      Use a.value.split(" \n").length.

      In both in cases, the textarea must be set with the right wrap
      attributes/CSS as to achieve a maximum browser compatibility.

      Hope this helps,

      --
      Bart

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: How to Count the number of lines?

        Anz wrote:
        Is there any javascript function for counting the number of lines
        inside a text area ?
        As for actual lines, independent of presentation:

        textarea.value. split(/\r?\n|\r/).length


        PointedEars
        --
        Anyone who slaps a 'this page is best viewed with Browser X' label on
        a Web page appears to be yearning for the bad old days, before the Web,
        when you had very little chance of reading a document written on another
        computer, another word processor, or another network. -- Tim Berners-Lee

        Comment

        • Evertjan.

          #5
          Re: How to Count the number of lines?

          Thomas 'PointedEars' Lahn wrote on 01 jul 2008 in comp.lang.javas cript:
          Anz wrote:
          >Is there any javascript function for counting the number of lines
          >inside a text area ?
          >
          As for actual lines, independent of presentation:
          >
          textarea.value. split(/\r?\n|\r/).length
          textarea.value. split(/\r?\n|\r/).length + 1



          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • Dr J R Stockton

            #6
            Re: How to Count the number of lines?

            In comp.lang.javas cript message <cd27072c-761f-4e82-a475-fd08860157c0@v1
            g2000pra.google groups.com>, Tue, 1 Jul 2008 00:39:12, Anz
            <anzbmuhammad@g mail.composted:
            >Is there any javascript function for counting the number of lines
            >inside a text area ?
            In addition to what others have written, ISTM that there is a question
            of whether blank lines within, before, or after non-blank lines should
            be counted.

            --
            (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
            Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
            Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
            Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)

            Comment

            • Evertjan.

              #7
              Re: How to Count the number of lines?

              Dr J R Stockton wrote on 01 jul 2008 in comp.lang.javas cript:
              In comp.lang.javas cript message
              <cd27072c-761f-4e82-a475-fd08860157c0@v1 g2000pra.google groups.com>,
              Tue, 1 Jul 2008 00:39:12, Anz <anzbmuhammad@g mail.composted:
              >>Is there any javascript function for counting the number of lines
              >>inside a text area ?
              >
              In addition to what others have written, ISTM that there is a question
              of whether blank lines within, before, or after non-blank lines should
              be counted.

              .... where a blank line not always is an empty line.


              =============== =============== =====
              <textarea id=t>



              qqq

              sss

              www

              </textarea>

              <script type='text/javascript'>

              var r;
              var t = document.getEle mentById('t');

              r = t.value.split(/\n/).length;
              alert(r); // 9
              r = t.value.split(/\n*/).length;
              alert(r); // 18
              r = t.value.split(/[\r\n]+/).length;
              alert(r); // 3
              r = t.value.split(/\n[\r\n]*/).length;
              alert(r); //4

              </script>
              =============== =============== ====

              18 ???

              What is happening here [IE7]?

              ;-)


              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • Bart Van der Donck

                #8
                Re: How to Count the number of lines?

                "Evertjan." wrote:
                <textarea id=t>
                >
                qqq
                >
                sss
                >
                www
                >
                </textarea>
                >
                <script type='text/javascript'>
                >
                var r;
                var t = document.getEle mentById('t');
                >
                r = t.value.split(/\n/).length;
                alert(r); // 9
                r = t.value.split(/\n*/).length;
                alert(r); // 18
                r = t.value.split(/[\r\n]+/).length;
                alert(r); // 3
                r = t.value.split(/\n[\r\n]*/).length;
                alert(r); //4
                >
                </script>
                =============== =============== ====
                >
                18  ???
                >
                What is happening here [IE7]?
                In your example,

                t.value.split(/\n*/).length

                is the same as

                t.value.length

                minus the number of newlines in 't', because a split on '\n*' can be
                semantically read as "splitting on a newline that is repeated zero or
                more times".

                --
                Bart

                Comment

                Working...