Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Seems like there is no build in tools to get this value. But we can calculate it. Assume that the line-height in textarea is a known value (you can explicitly define it in css). So the code to calculate the number of rows would like this:</p> <pre><code> var area = $('#myarea').get(0); //save the initial height of textarea var initialHeight = area.style.height; //set the height to 0 so scrollHeight will always return dynamic height area.style.height = '0px'; //here we assume that the line-height equals to 15 var numberOfRows = Math.floor(area.scrollHeight / 15); //restore textarea height area.style.height = initialHeight; console.log(numberOfRows); </code></pre> <p>Working example <a href="http://jsfiddle.net/J5UpF/" rel="nofollow">http://jsfiddle.net/J5UpF/</a></p> <p><strong>UPDATE</strong></p> <p>Just figured out the bug in Chrome with my code. I didn't take into account the scrollbar that appears on the right side of textarea. That leads to the wrong number of lines calculated sometimes. However it works like a charm in Firefox. To fix this issue we have to calculate the difference of scrollbars width and adjust the width of textarea accordingly:</p> <pre><code>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); </code></pre> <p>updated example <a href="http://jsfiddle.net/J5UpF/3/" rel="nofollow">http://jsfiddle.net/J5UpF/3/</a></p> <p><strong>UPDATE 2</strong></p> <p>A new awesome bug in Chrome is discovered! When using <code>placeholder</code> attribute Chrome calculates the number of rows for placeholder text. The workaround is simple: just backup the placeholder attribute when you entered some data into textarea and restore it when data is cleared.</p> <p>Updated example <a href="http://jsfiddle.net/J5UpF/4/" rel="nofollow">http://jsfiddle.net/J5UpF/4/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload