This is a ui element I created which takes inspiration from facebook’s excellent growing textarea. Or at least I believe facebook was one of the first to create this.
How it works is when text is typed into the textarea and it becomes longer than the textarea height, then the textarea will grow. I’m keeping this plugin in a github repository, fork it, clone it My Javascript Git Repository.
Demo
The key to making something like this work, is you need to create div element that copies the text from the textarea to the div. The div naturally will grow when it has more text in it. When it does, you get the height of the div element and apply it to the textarea.
Here’s how you would init the growing text box.
$("textarea").growing();
I’ve also added some options you can pass in, growning({maxHeight:300,minHeight:40,buffer:20}) the max and min height are pretty self explainitory. The buffer is the space below the text in the textarea, if you want an extra line below the currently typing line.
The Code
Currently I’m adding a class to the div called faketextarea so I can style it the same as the textarea. This is important becuase if one element has more padding than the other, then the heights will be messed up. and this plugin won’t work exactly right.
.faketextarea, textarea {
padding: 5px;
border: 1px solid #efefef;
}
/* Copyright (c) 2009 Jon Rohan (http://dinnermint.org)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version: 1.0.0
* Written with jQuery 1.3.2
*/
(function($){
$.fn.growing = function(options){
var settings = $.extend({
maxHeight: 400,
minHeight: 40,
buffer: 0
}, options);
return this.each(function(){
var textarea = $(this); //cache the textarea
var minh = textarea.height()>settings.minHeight?textarea.height():settings.minHeight;
var w = parseInt(textarea.width()||textarea.css("width")); //get the width of the textarea
var div = $("<div class='faketextarea' style='position:absolute;left:-10000px;width:" + w + "px;padding:" + textarea.css("padding") +";border:" + textarea.css("border") + ";'></div>");
textarea.after(div);
var resizeBox = function(){
var html = textarea.val().replace(/(<|>)/g, '').replace(/n/g,"<br>|");
if(html!=div.html()) {
div.html(html);
var h = div.height();
prevh = textarea.height();
var newh = h<=minh?minh:(h>settings.maxHeight?settings.maxHeight:h);
newh += settings.buffer;
if(newh>=settings.maxHeight) {
textarea.css("overflow","auto");
} else {
textarea.css("overflow","hidden");
}
textarea.css({"height":newh+"px"});
}
};
textarea.keydown(resizeBox);
textarea.keyup(resizeBox);
resizeBox();
});
};
})(jQuery);
Again if you want to keep up with an updated version of this plugin check it out in My Javascript Git Repository.
If you enjoyed reading this post, check out this post jQuery Ghost Textbox plugin.
Sweet! Thinking about adding this to GitHub…
Nice… but can we have a single line textarea in the beginning? Like a simple text input. Thanks.
I have been wrestling with this problem for a while, and while the invisible div is an interesting idea, I do feel like it still presents it’s problem with both performance and various UI glitches (http://dl.dropbox.com/u/18782/glitch1.png is one problem, and the other one is the slight delay perceived when it expands a line).
I am currently working on a solution which I find works pretty well, no hidden elements required, performance is very solid. It uses only the text area, and while it’s not quite perfect (yet) because in IE you are limited to only very particular line-heights (still trying to work out a more elegant solution), I hope to debut that solution soon, but I can’t at the moment as I am utilizing it in a project still in stealth mode.
Also because I am a Mootools coder, it won’t be something that would work in jQuery, although it could certainly be ported.
Cheers
Nice script! Thank you!
I finally got it released:
http://mootools.net/forge/p/dynamictextarea
Fixed all the problems with line-heights etc. You can check out an interactive demo at:
http://mootools.net/shell/P832d/3/
nice.. code.. tnx for sharing.