Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Last active August 29, 2015 14:06
Show Gist options
  • Save swvitaliy/3b3cdcaa65bbaafae2e8 to your computer and use it in GitHub Desktop.
Save swvitaliy/3b3cdcaa65bbaafae2e8 to your computer and use it in GitHub Desktop.
function setTextareaHeight(textarea, incCoeff) {
$textarea = $(textarea);
var lh = parseInt($textarea.css('line-height'), 10);
var sh = $textarea[0].scrollHeight;
var newHeight = sh + lh * incCoeff;
$textarea.height(lh);
if ($textarea.height() < newHeight) {
$textarea.height(newHeight);
}
}
$('.textarea').keydown(function(ev) {
if (ev.keyCode === 13) {
setTextareaHeight(this, 1);
} else if (ev.keyCode === 8) {
setTextareaHeight(this, -1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment