Finding number of lines in an html textarea

Kyle picture Kyle · Sep 13, 2010 · Viewed 31.5k times · Source

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!

Answer

Delan Azabani picture Delan Azabani · Sep 13, 2010

The number of lines in the textarea would be

textarea.value.match(/\n/g).length + 1