Autosize
Demo
Released under the MIT License, source on Github (changelog)
Install via NPM
npm install autosize
Browser compatibility
Chrome | Firefox | IE | Safari | iOS Safari | Android | Opera Mini |
---|---|---|---|---|---|---|
yes | yes | 9 | yes | yes | 4 | ? |
Usage
The autosize function accepts a single textarea element, or an array or array-like object (such as a NodeList or jQuery collection) of textarea elements.
// from a NodeList
autosize(document.querySelectorAll('textarea'));
// from a single Node
autosize(document.querySelector('textarea'));
// from a jQuery collection
autosize($('textarea'));
Methods
Name | Description |
---|---|
autosize.update(elements) |
Triggers the height adjustment for an assigned textarea element. Autosize will automatically adjust the textarea height on keyboard and window resize events. There is no efficient way for Autosize to monitor for when another script has changed the textarea value or for changes in layout that impact the textarea element.
|
autosize.destroy(elements) |
Removes Autosize and reverts any changes it made to the textarea element.
|
Life-cycle Events
Event | Description |
---|---|
autosize:resized |
This event is fired every time autosize adjusts the textarea height.
If you are using jQuery, you can use the on/off methods to listen to this event:
|
autosize:update |
Dispatch this event on a textarea element as an alternative to using the
|
autosize:destroy |
Dispatch this event on a textarea element as an alternative to using the
|
Trouble shooting / FAQ
Setting a min-height or max-height
Use CSS to specify a min-height and max-height for the textarea element. Once the height exceeds the max-height, Autosize will re-enable the vertical scrollbar.
The rows attribute can also be used to specify a minimum height. The rows attribute has a default value of 2
, so to make the textarea smaller than that you'll need to set the value to 1
. Example: <textarea rows='1'></textarea>
Fixing slow or sluggish performance
In Webkit, the default focus style for input and textarea elements includes outline-style: auto
, which has a blurred drop-shadow like appearance. Completely independent of Autosize, Safari is terrible at animating anything with this blur on it. My recommendation would be to use a non-blurred outline-style, and to remove any blurred box-shadow or other taxing CSS effect. For example:
input:focus, textarea:focus {
outline-style: solid;
outline-width: 2px;
}
Initial height is incorrect
Autosize needs to be able to calculate the width of the textarea element when it is assigned. JavaScript cannot accurately calculate the width of an element that has been detached from the DOM or had its display set to none. If you want to assign Autosize to a hidden textarea element that you plan to reveal later, be sure to either specify the pixel width of the element in your CSS, or use the autosize.update method after you reveal the textarea element.
Possible ways to resolve:
-
Specify an exact width for the textarea element in your stylesheet.
-
Delay assigning Autosize until the textarea has been revealed. Alternatively, wait until the user focuses the textarea element:
var ta = document.querySelector('textarea'); ta.addEventListener('focus', function(){ autosize(ta); });
-
Use the autosize.update method (or trigger the
autosize:update
event) after the element has been revealed.var ta = document.querySelector('textarea'); ta.style.display = 'none'; autosize(ta); ta.style.display = ''; // Call the update method to recalculate the size: autosize.update(ta);