Last Updated: November 19, 2020
·
51.18K
· moak

Resize Textarea to fit Content

When users type and add or remove new lines the textarea re-sizes to fit the same number of lines

$('#content').on( 'change keyup keydown paste cut', 'textarea', function (){
    $(this).height(0).height(this.scrollHeight);
}).find( 'textarea' ).change();

Check dat fiddle
http://jsfiddle.net/C8e4w/1/

Related protips:

jQuery: When to use $(document).ready() and when $(window).load()

2 Responses
Add your response

Excellent thanks :)
I have tweaked this a little to use it as an extension:

$.fn.extend({
autoresize: function () {
$(this).on('change keyup keydown paste cut', function () {
$(this).height(0).height(this.scrollHeight);
}).change();
}
});

$('#mytextarea').autoresize();

over 1 year ago ·

I signed up just tell you thank you for sharing, Mark. It was exactly what I needed. I added #content{overflow:hidden;} to get rid of the quick scroll bar that shows up when it resizes.

over 1 year ago ·