I'm writing a mobile web application where scrollbars are not displayed on the device's browser. Due to this, I'm trying to dynamically modify the height of the textarea to make it bigger, however I don't know of any way to actually get the line count on an html textarea. Any help would be greatly appreciated!
EDIT
So I realize now that it's not newlines per se, but actual line wrapping. So when one line finishes it wraps the text to the next line. It appears as if it is a new line. Any way to count the number of these? Thanks!
I have created a plugin to handle line counting and wrap detection in a
<textarea>
.I hope someone can use it.
Code on BitBucket
Sample Usage
Working Demonstration
Get scrollHeight, subtract top+bottom padding, divide by lineHeight.
This is an efficient and accurate method to count the number of lines in a text area, including wrapped lines.
Demo here
Maybe there is a way to get the "raw" number of "visual" lines. You should read the
scrollHeight
property of the textarea and divide it by the height of a line. Let's try.Start with this HTML:
Then:
I am not sure if that really works, just a mad theory.
The number of lines in the textarea would be
The number of characters allowed per line is dictated by the "cols" attribute of the textarea.
Assuming 80 characters per line, a good estimate may be:
Doesn't account for word-break and word-wrap.