Skip to content
jQuery plugin for elegant expanding textareas
JavaScript CSS
Latest commit 557d9a9 Jan 4, 2015 @domchristie domchristie Test defaults
Failed to load latest commit information.
libs remove toggle button. Issue #43 Apr 30, 2014
test Test defaults Jan 4, 2015
.gitignore Set up travis ci with grunt Jan 23, 2014
.jshintrc Update jshintrc and js files to satisfy changes Dec 14, 2014
ExpandingTextareas.jquery.json prep for 0.2.0 Dec 7, 2014
LICENSE add license Nov 4, 2011
bower.json prep for 0.2.0 Dec 7, 2014
expanding.js Fix autoinitialisation Dec 14, 2014
package.json Update grunt jshint and Gruntfile Dec 14, 2014

Expanding Textareas jQuery Plugin

Based off of work by Neil Jenkins that can be seen here:



Start with markup like this:

<script src=""></script>
<script src='PATH/TO/expanding.js'></script>
<textarea class='expanding'></textarea>

And that's it. The plugin finds textareas with the expanding class on page load and initializes them for you. These textareas will automatically resize now as the user changes the value.

If you'd like to change the initial selector to grab ALL textareas on load, you can change this property:

$.expanding.initialSelector = "textarea";


If you would prefer to initialize the textareas on your own, do something like this:

<script type='text/javascript'>

If you'd like to change the value by code and have it resize manually, you can do:



There aren't any options needed for this plugin. If your textarea has certain attributes, the plugin will handle them gracefully.

  • <textarea wrap=off></text>: wrapping will not happen, but if a newline is entered the height will be updated.
  • <textarea rows=10></text>: The plugin respects the rows attribute, adjusting the clone's min height accordingly.



    update: function() {
      // Textarea has been updated, size may have changed.



Once attached, the expanding behaviour can be removed as follows:



To test whether a jQuery selection has expanding behaviour:

$("#element").expanding('active'); // -> boolean

Note: this behaves like .hasClass(): it will return true if any of the nodes in the selection have expanding behaviour.


Plugin styles can be refreshed as follows:


This should be called after expanding textarea styles are updated, or box model dimensions are changed.

Textareas outside the DOM

The plugin 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. Calling expanding() on a textarea outside the DOM will have no effect.


You can style things how you'd like for the textarea, and they will automatically be copied over to the invisible pre tag, with the exception of margins (which are reset to 0, to ensure that the clone maintains the correct size and positioning).

Flash of unstyled content (FOUC) can be avoided by adding the following styles to your stylesheet (adjust the selector if necessary):

textarea.expanding {
  margin: 0;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
  width: 100%;

By default, the textarea will behave like a block-level element: its width will expand to fill its container. To restrict the textarea width, simply apply a width declaration to a parent element e.g. the textarea wrapper:

.expanding-wrapper {
   width: 50%;

See the demo to see the plugin in action.

Browser Support

This has been checked in Chrome, Safari, Firefox, IE8, and mobile Safari and it works in all of them.

How it works

See the original article for a great explanation of how this technique works.

The plugin will automatically find this textarea, and turn it into an expanding one. The final (generated) markup will look something like this:

<div class="expanding-wrapper">
  <textarea class="expanding"></textarea>
  <pre class="expanding-clone"><div></div></pre>

The way it works is that as the user types, the text content is copied into the div inside the pre (which is actually providing the height of the textarea). So it could look like this:

<div class="expanding-wrapper">
  <textarea class="expanding">Some Content\nWas Entered</textarea>
  <pre class="expanding-clone"><div>Some Content
  Was Entered</div></pre>

Running Tests Locally

Browser: open test/index.html

Command line: make sure you have installed node.js, and grunt-cli, then run:

$ npm install

Followed by:

$ grunt test

Continuous Deployment

View tests online at:

Build Status

Something went wrong with that request. Please try again.