By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,928 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,928 IT Pros & Developers. It's quick & easy.

How to Count the number of lines?

P: n/a
Anz
Is there any javascript function for counting the number of lines
inside a text area ?
Jul 1 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Jul 1, 3:39*am, Anz <anzbmuham...@gmail.comwrote:
Is there any javascript function for counting the number of lines
inside a text area ?
try:
a.value.split("\n").length
Jul 1 '08 #2

P: n/a
RoLo wrote:
On Jul 1, 3:39*am, Anz <anzbmuham...@gmail.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 <textareashould 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
Jul 1 '08 #3

P: n/a
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
Jul 1 '08 #4

P: n/a
Thomas 'PointedEars' Lahn wrote on 01 jul 2008 in comp.lang.javascript:
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)
Jul 1 '08 #5

P: n/a
In comp.lang.javascript message <cd27072c-761f-4e82-a475-fd08860157c0@v1
g2000pra.googlegroups.com>, Tue, 1 Jul 2008 00:39:12, Anz
<an**********@gmail.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.demon.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)
Jul 1 '08 #6

P: n/a
Dr J R Stockton wrote on 01 jul 2008 in comp.lang.javascript:
In comp.lang.javascript message
<cd27072c-761f-4e82-a475-fd08860157c0@v1 g2000pra.googlegroups.com>,
Tue, 1 Jul 2008 00:39:12, Anz <an**********@gmail.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.getElementById('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)
Jul 2 '08 #7

P: n/a
"Evertjan." wrote:
<textarea id=t>

qqq

sss

www

</textarea>

<script type='text/javascript'>

var r;
var t = document.getElementById('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
Jul 2 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.