Skip to content

Instantly share code, notes, and snippets.

@haggen
Last active August 29, 2015 14:03
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 haggen/18645f9adf0ea162bab4 to your computer and use it in GitHub Desktop.
Save haggen/18645f9adf0ea162bab4 to your computer and use it in GitHub Desktop.
Auto size (height) for <textarea>
;(function() {
var sham = $('<pre></pre>');
sham.appendTo('body');
$(document).on('input change focus', 'textarea', function() {
var textarea = $(this);
sham.css('width', textarea.outerWidth());
sham.text(this.value + '&nbsp;');
sham.attr('class', 'textarea-sham ' + textarea.attr('class'));
textarea.css('height', sham.outerHeight());
});
})();
// Add this to your stylesheet (SCSS required)
.textarea-sham {
@extend textarea;
display: none;
height: auto;
white-space: pre-line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment