Skip to content

Instantly share code, notes, and snippets.

Created July 19, 2015 09:16
Show Gist options
  • Save anonymous/214af22fe4860dd7ad76 to your computer and use it in GitHub Desktop.
Save anonymous/214af22fe4860dd7ad76 to your computer and use it in GitHub Desktop.
BEST Textarea auto-expand
<textarea class='autoExpand' rows='3' data-min-rows='3' placeholder='Auto-Expanding Textarea'></textarea>
$(document)
.one('focus.textarea', '.autoExpand', function(){
var savedValue = this.value;
this.value = '';
this.baseScrollHeight = this.scrollHeight;
this.value = savedValue;
})
.on('input.textarea', '.autoExpand', function(){
var minRows = this.getAttribute('data-min-rows')|0,
rows;
this.rows = minRows;
console.log(this.scrollHeight , this.baseScrollHeight);
rows = Math.ceil((this.scrollHeight - this.baseScrollHeight) / 17);
this.rows = minRows + rows;
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
body{ background:#728EB2; }
textarea{
display:block;
box-sizing: padding-box;
overflow:hidden;
padding:10px;
width:250px;
font-size:14px;
margin:50px auto;
border-radius:8px;
border:6px solid #556677;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment