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.
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
>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)
>>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]?
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".
Comment