Form element autosizing,
the way it should be.



More examples in the testsuite.


Just include the script anywhere in the page:

<script src="stretchy.min.js" async></script>

By default, it applies to all <textarea>s, <select> menus with no size attribute and <input> elements that are text fields (e.g. with no type attribute, or with one equal to text, tel, email, url).

To limit that set further you can use the data-stretchy-filter attribute, on any element. Note that this means you can use the attribute on the <script> element that calls Stretchy itself, in which case you can also shorten its name to data-filter.

For example, to restrict it to elements that either have the foo class or are inside another element that does, you could use data-stretchy-filter=".foo, .foo *" on an element or call Stretchy like this:

<script src="stretchy.min.js" data-filter=".foo, .foo *" async></script>

If you specify the data-stretchy-filter attribute on multiple elements, the last value (in source order) wins. data-filter directly on Stretchy’s <script> element takes priority over any data-stretchy-filter declaration.

If you want to avoid modifying the markup, you can use JavaScript instead:

Stretchy.selectors.filter = ".foo, .foo *";

Note that to prevent Stretchy from being applied to elements that don’t match the selector at all, you would need to run that line before DOMContentLoaded fires.

JavaScript API

Stretchy has a spartan API, since in most cases you don’t need to call it at all. Stretchy works via event delegation and detects new elements via mutation observers, so you do not need to call any API methods for adding new elements via scripting (e.g. AJAX).

If needed, these are Stretchy’s API methods:

Autosize one element based on its content. Note that this does not set up any event listeners, it just calculates and sets the right dimension (width or height, depending on the type of control) once.
Apply Stretchy.resize() to a collection of elements, or all Stretchy is set to apply to, if no argument is provided.
Can Stretchy be used on this particular element? (checks if element is in the DOM, if it's of the right type and if it matches the selector filter provided by data-stretchy-selector, if the attribute is set.
Boolean. Set to false to temporarily disable Stretchy.

Browser Support Notes