Skip to content

Instantly share code, notes, and snippets.

@alexwebgr
Created May 30, 2019 08:43
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 alexwebgr/7cfcb0a8fb5d39fef70caaf5c02ef9a6 to your computer and use it in GitHub Desktop.
Save alexwebgr/7cfcb0a8fb5d39fef70caaf5c02ef9a6 to your computer and use it in GitHub Desktop.
resize a textarea as you type
$("textarea.growable")
.on('keyup paste input', function () {
let self = $(this);
let offset = self.innerHeight() - self.height();
if (self.innerHeight() < this.scrollHeight) {
//Grow the field if scroll height is smaller
self.height(this.scrollHeight - offset);
} else {
//Shrink the field and then re-set it to the scroll height in case it needs to shrink
self.height(1);
self.height(this.scrollHeight - offset);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment