Forum Moderators: open

Message Too Old, No Replies

Counting the number of line in a form textarea

         

erikcw

2:07 am on Jun 3, 2004 (gmt 0)

10+ Year Member



Hi all,

I'm trying to figure out how to count the number of lines (\n) in a form textarea, and then print that number to the page. It also needs to update as more lines of text are being added. (i would prefer to echo the count as plain text rather than in another textbox).

The only scripts I've been able to find count words in a textarea.

Any ideas?

babloo

6:36 am on Jun 3, 2004 (gmt 0)

10+ Year Member



I think you have to use the server side script to find out this. Convert all the newline in the textarea to the HTML line breaks (<BR>). Assign the entire text as array and then split the array with <br> and then take the count of the array. I hope this will help you.

john_k

6:53 am on Jun 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you could use the split function to break the text into an array. Then check the array's length to get the line count.

[edit]
this can all be done in javascript on the client.
[/edit]

Bernard Marx

8:59 am on Jun 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm. Using a server-side script, and replacing all \n with <br> is a little on the overkill side. Here's a demo of john_k's method..

[blue]<html><head>
<title>Banana</title>
<script>
function countLines()
{
var area = document.getElementById("texta")
// trim trailing return char if exists
var text = area.value.replace(/\s+$/g,"")
var split = text.split("\n")
return split.length
}
</script>
</head>
<body>
<textarea id="texta" cols="30" rows="8">
I think you could use the split function to break
the text into an array.
Then check the array's length to get
the line count.
</textarea>
<br>
<button onclick="alert(countLines())">count</button>
</body></html>[/blue]

This will only count the line breaks entered by the user.
It won't count the lines caused by wrapping.

erikcw

5:08 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



Bernard Marx,

Thanks for the script, but I have one question. I'm going to put onChange="countlines()" in the textarea code so it updates the count automatically.

However I can't figure out how to have it print to the page. Perhaps in between <span></span>? How do I get it to print?

Thanks!

erikcw

5:38 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



Ok, I figured out the dhtml part - but I want it to update more freauently - my code is only changing the count when someone makes the textarea active, or inactive - I want it to update after every line the user enters -or whenever any change is made.


<html><head>
<title>Banana</title>
<script>
function countLines() {
var area = document.getElementById("texta")
// trim trailing return char if exists
var text = area.value.replace(/\s+$/g,"")
var split = text.split("\n")
return split.length }
</script>

</head>
<body>
<textarea id="texta" cols="30" rows="8" onChange="document.all.count.innerHTML = 2000 - countLines()">
I think you could use the split function to break the text into an array.
Then check the array's length to get the line count.
</textarea>

<br>
<div id="count">2000</div>
</body></html>

Should I be using something other than onChange?

chadmg

6:01 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



You should use onkeyup instead of onchange. And you can send the count to an input field or an html tag...

document.inputField.value = count;
doument.divTag.innerHTML = count;

Bernard Marx

6:16 pm on Jun 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. onChange is only fired when (the textbox loses focus && the content has changed).

Seriously amended

Yes, chadmg, putting the onKeyPress ¦¦ Up on the element is good. I was about to put it on the document. Now it only fires when the textbox has focus. Also we can send a ref to the function, which will make it more efficient (useful if it runs on every key event).

<textarea onKeyPress="countLines([blue]this[/blue])">

Then the function is amended:

function countLines([blue]area[/blue]) 
{
// trim trailing return char if exists
var text = area.value.replace(/\s+$/g,"")
var split = text.split("\n")
return split.length
}

You could go further, and have the script only running the function when the return ky has been pressed - a few cross-browser things enter into the equation there.

You may also want to remove the bit that trims the content end

.replace(/\s+$/g,"")
. I don't know what you're requirements are there.

erikcw

9:50 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



That new code works great! But how do I get it to execute the function as soon as the page loads so I can give the user a count of how many lines they are starting with?

Thanks!

Bernard Marx

11:31 pm on Jun 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<body onload="countLines(document.getElementById('texta'))">

or

window.onload = function(){ countLines(document.getElementById('texta')) }

erikcw

12:52 am on Jun 4, 2004 (gmt 0)

10+ Year Member



That did the trick (body onload)

Thanks!