Skip to content

Instantly share code, notes, and snippets.

@Victa
Created January 12, 2012 10:07
Show Gist options
  • Select an option

  • Save Victa/1599676 to your computer and use it in GitHub Desktop.

Select an option

Save Victa/1599676 to your computer and use it in GitHub Desktop.
Textarea Auto Resize
/*
* Auto resizing textarea
* Usage: <textarea class="autoresize"></textarea>
*/
var textareaAutoResize = function textareaAutoResize(minHeight){
var txt = $('.autoresize'),
hiddenDiv = $(document.createElement('div')),
content = null,
mH = minHeight || 50;
txt.css({overflow:'hidden',minHeight:mH});
hiddenDiv.css({
display: 'none', whiteSpace: 'pre-wrap', minHeight: mH,
overflow: 'hidden', wordWrap: 'break-word', fontFamily: txt.css('font-family'),
fontSize: txt.css('font-size'), padding: txt.css('padding'), width: txt.css('width')
});
$('body').append(hiddenDiv);
txt.bind('keyup', function() {
content = txt.val();
content = content.replace(/\n/g, '<br>');
hiddenDiv.html(content);
txt.css('height', hiddenDiv.height());
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment