Skip to content

Instantly share code, notes, and snippets.

@numbcoder
Created November 22, 2011 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numbcoder/1385367 to your computer and use it in GitHub Desktop.
Save numbcoder/1385367 to your computer and use it in GitHub Desktop.
Textarea auto resize
$.fn.textResizer = function(options) {
var el = $(this), h;
var settings = {
minHeight: el.height(),
maxHeight: 300,
duration: 100
}
if (options) {
$.extend(settings, options);
}
var clone = el.clone().removeAttr('id').removeAttr('class').css({position:'absolute', top:'-9999em',left:'-9999em',width: el.width(), height: 'auto'}).appendTo('body');
$(this).keyup(function(e) {
h = clone.val(el.val()).height(0).scrollTop(10000).scrollTop() + 16;
h = Math.min(Math.max(settings.minHeight, h), settings.maxHeight);
if (el.height() != h) {
if($.browser.msie) {
el.height(h);
} else {
el.stop(1,1).animate({height: h}, settings.duration);
}
}
});
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment