I took the idea from Jake Feysel's answer, and it not only works to expand, but also to compress the text box in case of less content.
HTML
<textarea id="foo" rows="1" cols="40"></textarea> <div id="stat"></div>
It also displays the ideal number of lines needed according to the contents of the div divider
CSS
β#foo { resize:none; }β
This is required because once the text field has been modified using this handler, the code stops working
Js
$(function () { $("#foo").on("keyup", function (e) { var idealRows = parseInt($(this).val().length/$(this).attr('cols'))+ ($(this).val().split("\n").length-1)+1 ; var rows = $(this).attr('rows');
Demo: http://jsfiddle.net/roopunk/xPdaP/3/
Note that he also takes care of the scroll bar that appears between keyDown and keyUp. IMO: This is a kind of audition.
Note I hope this helps! :)
source share