Sign in to follow this  
GameMasterXL

[web] Counting textarea lines?

Recommended Posts

Using javascript, you could just go through all the characters and see if it is a line break:

function count()
{
var str = document.getElementById("theTextArea").value;
var num = 0;

for (i = 0; i < str.length; i++)
{
if (str.charAt(i) == '\n')
num++;
}

return num;
}

Above code could be optimized a little, it's just to give you an idea.

Share this post


Link to post
Share on other sites
Ah that did the trick thanku so so much for the help. One thing do you know how i can get it to make a new number on a new line when i just press enter?

my code here:

function count()
{
var str = document.getElementById("SyntaxView").value;
var num = 0;

for (i = 0; i < str.length; i++)
{
if (str.charAt(i) == '\n')
num++;
}
document.getElementById('lines').innerHTML=document.getElementById('lines').innerHTML+num+"\n";
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this