You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expanding can be installed via NPM, Bower, or by downloading the script located at dist/expanding.js. It can be required via CommonJS, AMD, or as a global (e.g. window.Expanding).
via npm:
npm install expanding-textareas ... var Expanding = require('expanding-textareas')
via bower:
bower install expanding-textareas
The library is also available as a jQuery plugin (see below).
Usage
Expanding is a constructor which takes a textarea DOM node as its only argument:
That's it! The textarea will now expand as the user types.
update
Updates the textarea height. This method is called automatically when the user types, but when setting the textarea content programmatically, it can be used to ensure the height expands as needed. For example:
textarea.value='Hello\nworld!'// Height is not yet updated expanding.update()// Height is now updated
refresh
Resets the styles of the internal elements to match those of the textarea. This may be useful if the textarea has percentage padding, and the browser window resizes, or if the textarea styles change after Expanding is called.
destroy
Removes the behavior. It unbinds the internal event listeners and removes the DOM nodes created by the library.
jQuery Plugin
Download the jQuery plugin located at dist/expanding.jquery.js, and include it in your page (after jQuery). For example:
To disable auto-initialization, set $.expanding.autoInitialize to false:
$.expanding = { autoInitialize: false }
Manual Initialization
To manually initialize the plugin call expanding() on the jQuery selection. For example to apply the behavior to all textareas:
$('textarea').expanding()
Options
destroy
'destroy' will remove the behavior:
$('textarea').expanding('destroy')
active
'active' will check whether it has the expanding behavior applied:
$('textarea').expanding('active')// returns true or false
Note: this behaves like .hasClass(): it will return true if any of the nodes in the selection have the expanding behaviour.
refresh
'refresh' will update the styles (see above for more details):
$('textarea').expanding('refresh')
Caveats
Textareas must be visible for the library to function properly. The library creates a textarea clone with identical dimensions to that of the original. It therefore requires that the textarea be in place in the DOM for these dimensions to be correct.
Any styling applied to the target textarea will be maintained with the exception of margins and widths. (Margins are reset to 0 to ensure that the textarea maintains the correct size and positioning.)
After the expanding behavior has been applied, the textarea will appear like a block-level element: its width will expand to fill its container. To restrict the textarea width, apply a width declaration to a parent element. The library's wrapper (.expanding-wrapper) element may be useful in this case:
.expanding-wrapper { width:50%; }
Flash of unstyled content can be avoided by adding the following styles (adjust the selector as necessary):
The library aims to support modern versions of the following browsers: Chrome, Firefox, IE (9+), Opera, and Safari (incl. iOS). View the test suite to see if check if your browser is fully supported. (If there are no failures then you're good to go!)
Development & Testing
This library has been developed with ES2015 modules and bundled with Rollup. To get started with development, first clone the project: