There was another thread about this, which I've tried. But there is one problem: the textarea
doesn't shrink if you delete the content. I can't find any way to shrink it to the correct size - the clientHeight
value comes back as the full size of the textarea
, not its contents.
The code from that page is below:
function FitToContent(id, maxHeight)
{
var text = id && id.style ? id : document.getElementById(id);
if ( !text )
return;
var adjustedHeight = text.clientHeight;
if ( !maxHeight || maxHeight > adjustedHeight )
{
adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
if ( maxHeight )
adjustedHeight = Math.min(maxHeight, adjustedHeight);
if ( adjustedHeight > text.clientHeight )
text.style.height = adjustedHeight + "px";
}
}
window.onload = function() {
document.getElementById("ta").onkeyup = function() {
FitToContent( this, 500 )
};
}
Here is an angularjs directive for panzi's answer.
HTML:
The jQuery solution is to set the height of the textarea to 'auto', check the scrollHeight and then adapt the height of the textarea to that, every time a textarea changes (JSFiddle):
If you're dynamically adding textareas (through AJAX or whatever), you can add this in your $(document).ready to make sure all textareas with class 'autoheight' are kept to the same height as their content:
Tested and working in Chrome, Firefox, Opera and IE. Also supports cut and paste, long words, etc.
The best solution (works and is short) for me is:
It works like a charm without any blinking with paste (with mouse also), cut, entering and it shrinks to the right size.
Please take a look at jsFiddle.
The following works for cutting, pasting, etc., regardless of whether those actions are from the mouse, a keyboard shortcut, selecting an option from a menu bar ... several answers take a similar approach but they don't account for box-sizing, which is why they incorrectly apply the style
overflow: hidden
.I do the following, which also works well with
max-height
androws
for minimum and maximum height.For absolute completeness, you should call the
adjust
function in a few more circumstances:textarea
changes with window resizing, or other events that change the width of the textareatextarea
'sdisplay
style attribute changes, e.g. when it goes fromnone
(hidden) toblock
textarea
is changed programmaticallyNote that using
window.getComputedStyle
or gettingcurrentStyle
can be somewhat computationally expensive, so you may want to cache the result instead.Works for IE6, so I really hope that's good enough support.
This code works for pasting and select delete also.
Here is the JSFiddle
As a different approach, you can use a
<span>
which adjusts its size automatically. You will need make it editable by adding thecontenteditable="true"
property and you're done:The only issue with this approach is that if you want to submit the value as part of the form, you'll have to do so by yourself in JavaScript. Doing so is relatively easy. For example, you can add a hidden field and in the
onsubmit
event of the form assign the value of thespan
to the hidden field which will be then automatically submitted with the form.