Check out this demonstration of a text area that expands based on the content you input:
http://jsfiddle.net/janjarfalk/r3Ekw/
The functionality of the elastic text area is made possible by an external script called jquery.elastic.source.js, which can be found here:
Now that you have explored these resources, let's delve into how it works. The script includes an "update" function that simply adds whitespace when reaching the end of a row like so:
// Add an extra white space so new rows are added when you are at the end of a row.
$twin.html(textareaContent+' ');
Additionally, there are specific functions for setting the width and height of the text area dynamically:
// Updates the width of the twin. (solution for textareas with widths in percent)
function setTwinWidth(){
var curatedWidth = Math.floor(parseInt($textarea.width(),10));
if($twin.width() !== curatedWidth){
$twin.css({'width': curatedWidth + 'px'});
// Update height of textarea
update(true);
}
}
// Sets a given height and overflow state on the textarea
function setHeightAndOverflow(height, overflow){
var curratedHeight = Math.floor(parseInt(height,10));
if($textarea.height() !== curratedHeight){
$textarea.css({'height': curratedHeight + 'px','overflow':overflow});
}
}
All of this functionality is part of the elastic feature. Hopefully, this breakdown sheds some light and proves useful to you.