Link to home
Start Free TrialLog in
Avatar of cplus_man
cplus_man

asked on

Get number of lines in a text area

Is there any direct way to find out (using JavaScript) how many lines of user-entered text there are in a multi-line text area on an HTML form?
Avatar of knightEknight
knightEknight
Flag of United States of America image

If by that you mean how many \n characters, you can do this:

var lines = document.nameOfYourForm.nameOfYourTextarea.value.split("\n").length;
alert(lines);
Avatar of cplus_man
cplus_man

ASKER

Is there also a way to count lines where the text was automatically wrapped? My textarea's wrap attribute is set to 'physical', and I'm using IE 6.
assuming no \n characters:

var lines = Math.round( document.nameOfYourForm.nameOfYourTextarea.value.length / (1*document.nameOfYourForm.nameOfYourTextarea.cols) );
I guess IE 6 does not insert \n characters at wrapping points no matter what the wrap attribute is set to. Is that true? I have tried using both 'hard' and 'physical' for the wrap attribute on my textarea and IE still never seems to put the \n's at the wrapping points. That issue has been bugging me because it would be very helpful in solving this problem.
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I have combined both of the above approaches. It works, but it's not 100% accurate. But I guess it's the best I'll be able to do. Thanks for your help.