Auto-height textarea using as minimum JS as possible.
A Pen by Yair Even Or on CodePen.
Auto-height textarea using as minimum JS as possible.
A Pen by Yair Even Or on CodePen.
<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; | |
} |