Skip to content

Instantly share code, notes, and snippets.

@orioltf
Last active December 12, 2015 05:28
Show Gist options
  • Save orioltf/4721831 to your computer and use it in GitHub Desktop.
Save orioltf/4721831 to your computer and use it in GitHub Desktop.
#JQUERY: Autoresize textarea. The textarea will resize itself automatically as you add/remov content. For sure can be improved! http://codepen.io/orioltf/pen/euImv
<textarea id="comment" rows="0" cols="50"></textarea>
(function($) {
$.fn.autoResizeTextarea = function() {
return this.each(function() {
if(this.tagName.toLowerCase() != "textarea") return;
var rows = parseInt(this.getAttribute("rows"));
if(rows < 2) this.setAttribute("rows", 2);
else rows--;
$(this).on('keyup', function() {
var textLength = this.value.split("\n").length;
if(rows < textLength) {
console.log('1');
this.setAttribute("rows", textLength+1);
} else {
console.log('2');
this.setAttribute("rows", rows+1);
}
});
});
}
})(jQuery);
$('#comment').autoResizeTextarea();
@orioltf
Copy link
Author

orioltf commented Feb 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment