Form element autosizing,
the way it should be.
min/max-width/height
constraints, transitionsJust 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, as long as this is set before DOMContentLoaded
fires:
Stretchy.selectors.filter = ".foo, .foo *";
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:
Stretchy.resize()
to a collection of elements, or all Stretchy is set to apply to, if no argument is provided.data-stretchy-selector
, if the attribute is set.false
to temporarily disable Stretchy.Stretchy.resize()
on new elements. <input>
elements, due to it misreporting scrollWidth
. If this matters to you, you can use this polyfill by Greg Whitworth (under development).