How to Count the number of lines?

A

Anz

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

Bart Van der Donck

RoLo said:
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 <textarea> should 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,
 
T

Thomas 'PointedEars' Lahn

Anz said:
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
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 01 jul 2008 in comp.lang.javascript:
As for actual lines, independent of presentation:

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

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

Dr J R Stockton

In comp.lang.javascript message <cd27072c-761f-4e82-a475-fd08860157c0@v1
g2000pra.googlegroups.com>, Tue, 1 Jul 2008 00:39:12, Anz
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.
 
E

Evertjan.

Dr J R Stockton wrote on 01 jul 2008 in comp.lang.javascript:
In comp.lang.javascript message


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]?

;-)
 
B

Bart Van der Donck

Evertjan. said:
<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".
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top