<textarea rows="5"></textarea>
<textarea>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae tempora nemo autem ex beatae vel assumenda facere aliquam dicta aspernatur sapiente enim nostrum dolore placeat sint voluptates alias fugiat porro.</textarea>
<textarea rows="15"></textarea>
CSS
textarea { margin: 1em; outline: none; }
JavaScript
;(function($){
//pass in just the context as a $(obj) or a settings JS object
$.fn.autogrow = function(opts) {
var that = $(this).css({overflow: 'hidden', resize: 'none'}) //prevent scrollies
, selector = that.selector
, defaults = {
context: $(document) //what to wire events to
, animate: true //if you want the size change to animate
, speed: 200 //speed of animation
, fixMinHeight: true //if you don't want the box to shrink below its initial size
, cloneClass: 'autogrowclone' //helper CSS class for clone if you need to add special rules
, onInitialize: false //resizes the textareas when the plugin is initialized
}
;
opts = $.isPlainObject(opts) ? opts : {context: opts ? opts : $(document)};
opts = $.extend({}, defaults, opts);
that.each(function(i, elem){
var min, clone;
elem = $(elem);
//if the element is "invisible", we get an incorrect height value
//to get correct value, clone and append to the body.
if (elem.is(':visible') || parseInt(elem.css('height'), 10) > 0) {
min = parseInt(elem.css('height'), 10) || elem.innerHeight();
} else {
clone = elem.clone()
.addClass(opts.cloneClass)
.val(elem.val())
.css({
position: 'absolute'
, visibility: 'hidden'
, display: 'block'
})
;
$('body').append(clone);
min = clone.innerHeight();
clone.remove();
}
if (opts.fixMinHeight) {
elem.data('autogrow-start-height', min); //set min height
}
elem.css('height', min);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.