473,809 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Count the number of lines?

Anz
Is there any javascript function for counting the number of lines
inside a text area ?
Jul 1 '08 #1
7 17636
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
Jul 1 '08 #2
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
Jul 1 '08 #3
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
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)
Jul 1 '08 #5
In comp.lang.javas cript message <cd27072c-761f-4e82-a475-fd08860157c0@v1
g2000pra.google groups.com>, Tue, 1 Jul 2008 00:39:12, Anz
<an**********@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)
Jul 1 '08 #6
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 <an**********@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)
Jul 2 '08 #7
"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
Jul 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2186
by: deko | last post by:
Below are the contents file that has the IP address and time of visit of visitors to a website. 68.122.69.241|1089822686 68.122.69.241|1089823630 68.122.69.241|1089823638
22
61422
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: in_file = raw_input("What is the name of the file you want to open: ") in_file = open("test.txt","r")
1
4966
by: laredotornado | last post by:
Hello, I am trying to decipher an XSL sheet that is not my own, and yes, I'm a newbie. <xsl:template match="sentence"> <xsl:variable name="lines" select="count(line) + count(preceding::line)"/> <xsl:copy> <xsl:attribute name="sectionnumber"><xsl:value-of select="floor($lines div 15) + 1"/></xsl:attribute> <xsl:apply-templates/>
3
2966
by: Matthias Haffke | last post by:
Ok, this is a tricky question for the pro's: My access sheet: line, id a, id b, val% ---------------- 1, a, ac, 0.04 2, a, ac, 0.28 3, a, ac, 0.015 4, a, ac, 0.205
3
1543
by: Bruce | last post by:
Is there any mechanism in vs 2005 to have the IDE report the number of lines of source code in a C# project either per file or preferably across an entire project? Thanks, Bruce
23
2775
by: Paul Mars | last post by:
I need to limit multiline textbox to 3 lines of text and if there are less then 3 lines when leaving, add empty line holders. How can I do this?? Thanks, paul
4
1738
by: tommcd24 | last post by:
Does anyone know of a utility or VS add-in to count code lines in a project or solution? I just had to provide 50 pages of code for copyright and had initially thought that would be nearly the entire program only to discover that I had over 60 pages of code in just two classes. So now I'm curious how many lines of code are in the project... Thanks in advance for any information.
68
6838
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
13
4792
by: humaid | last post by:
hi,guys i have done a program to count the number of bigrams. i have taken a input file by using @ARGV,then icounted the number of lines in the file,using the split function i splited the sentence and pushed it in an array.and counted how many times the word is repeated.similarly,this is for single word,similarly i have paired two words from the array,and now i want to count the number of occurence of this,please help me out of...
3
2715
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word. #include <fstream> #include <iostream> #include <string> #include <cstdlib> using namespace std;
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.