There seems to be no built-in tools to get this value. But we can calculate it. Suppose the line height in textarea is a known value (you can explicitly define it in css). Thus, the code for calculating the number of lines will look like this:
var area = $('#myarea').get(0);
Working example http://jsfiddle.net/J5UpF/
UPDATE
Just calculated the error in Chrome with my code. I did not take into account the scroll bar that appears on the right side of the text box. This results in the wrong number of lines computed sometimes. However, it works like a charm in Firefox. To fix this problem, we need to calculate the difference in the width of the scrollbar and adjust the width of the textarea accordingly:
var area = $('#myarea').get(0); var $area = $('#myarea'); //save the initial height of textarea var initialHeight = area.style.height; var scrollbarWidthInitial = area.offsetWidth - area.clientWidth; //set the height to 0 so scrollHeight will always return dynamic height area.style.height = '0px'; var scrollbarWidth = area.offsetWidth - area.clientWidth; var scrollbarWidthDiff = scrollbarWidth - scrollbarWidthInitial; $area.width($area.width() + scrollbarWidthDiff); //here we assume that the line-height equals to 15 var numberOfRows = Math.floor(area.scrollHeight / 15); //restore textarea height area.style.height = initialHeight; $area.width($area.width() - scrollbarWidthDiff); console.log(numberOfRows);
updated example http://jsfiddle.net/J5UpF/3/
UPDATE 2
New terrific bug found in Chrome! When using the placeholder attribute, Chrome calculates the number of lines for the placeholder text. The workaround is simple: just back up the placeholder attribute when you entered some data in textarea and restore it when the data is cleared.
Updated example http://jsfiddle.net/J5UpF/4/
source share