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 )
};
}
I Don't know if anyone mention this way but in some cases it's possible to resize the height with rows Attribute
Demo
A bit corrections. Works perfectly in Opera
An even simpler, cleaner approach is this:
// and the CSS
All that is needed is to add the
.auto-height
class to anytextarea
you want to target.Tested in FF, Chrome and Safari. Let me know if this doesn't work for you, for any reason. But, this is the cleanest and simplest way I've found this to work. And it works great! :D
You can use JQuery to expand the
textarea
while typing:There is a slightly different approach.
The idea is to copy the text from
textarea
into thepre
and let CSS make sure that they have the same size.The benefit is that frameworks present simple tools to move text around without touching any events. Namely, in AngularJS you would add a
ng-model="foo" ng-trim="false"
to thetextarea
andng-bind="foo + '\n'"
to thepre
. See a fiddle.Just make sure that
pre
has the same font size as thetextarea
.You're using the higher value of the current clientHeight and the content scrollHeight. When you make the scrollHeight smaller by removing content, the calculated area can't get smaller because the clientHeight, previously set by style.height, is holding it open. You could instead take a max() of scrollHeight and a minimum height value you have predefined or calculated from textarea.rows.
In general you probably shouldn't really rely on scrollHeight on form controls. Apart from scrollHeight being traditionally less widely-supported than some of the other IE extensions, HTML/CSS says nothing about how form controls are implemented internally and you aren't guaranteed scrollHeight will be anything meaningful. (Traditionally some browsers have used OS widgets for the task, making CSS and DOM interaction on their internals impossible.) At least sniff for scrollHeight/clientHeight's existance before trying to enable the effect.
Another possible alternative approach to avoid the issue if it's important that it work more widely might be to use a hidden div sized to the same width as the textarea, and set in the same font. On keyup, you copy the text from the textarea to a text node in hidden div (remembering to replace '\n' with a line break, and escape '<'/'&' properly if you're using innerHTML). Then simply measuring the div's offsetHeight will give you the height you need.