• Stars
    star
    261
  • Rank 156,602 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jQuery plugin for elegant expanding textareas

ExpandingTextareas

Build Status

An elegant approach to making textareas automatically grow. Based on Expanding Text Areas Made Elegant by Neil Jenkins.

Installation

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:

var textarea = document.querySelector('textarea')
var expanding = new Expanding(textarea)

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:

var textarea = document.querySelector('textarea')
var expanding = new Expanding(textarea)

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:

<script src="http://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src='PATH/TO/expanding.jquery.js'></script>

Then, include the expanding class in any textarea you wish to add the behavior to:

<textarea class="expanding"></textarea>

The plugin will attach the behavior to every .expanding textarea when the DOM is ready.

Customizing the Initial Selector

To change the selector used for automatic initialization, modify $.expanding.initialSelector. For example:

$.expanding = {
  initialSelector: '[data-behavior=expanding]'
}

Disabling Automatic Initialization

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):

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

Browser Support

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:

git clone [email protected]:bgrins/ExpandingTextareas.git

Then navigate to the project and install the dependencies:

cd ExpandingTextareas
npm install

To bundle the source files:

npm run build

And finally to test:

npm test

Run the tests in a browser by opening test/index.html.

More Repositories

1

TinyColor

Fast, small color manipulation and conversion for JavaScript
JavaScript
5,040
star
2

devtools-snippets

A collection of helpful snippets to use inside of browser devtools
HTML
2,958
star
3

videoconverter.js

[UNMAINTAINED] Convert videos in your browser
JavaScript
2,344
star
4

spectrum

The No Hassle JavaScript Colorpicker
JavaScript
2,315
star
5

javascript-astar

A* Search / Pathfinding Algorithm in Javascript
JavaScript
1,307
star
6

filereader.js

A lightweight wrapper for the JavaScript FileReader interface
JavaScript
413
star
7

bindWithDelay

jQuery Plugin For Delayed Event Execution
JavaScript
151
star
8

InstantSprite

A fast browser based CSS Sprite Generator
JavaScript
141
star
9

nativesortable

Use native drag and drop APIs to provide sortable items. No dependancies.
JavaScript
87
star
10

colorwheel-1k

HSV colorpicker entry for js1k
JavaScript
31
star
11

ui.anglepicker

A jQuery UI Angle Picker Widget
JavaScript
30
star
12

DrawWindow

A Javascript implementation for canvas.drawWindow
JavaScript
29
star
13

devtools-demos

JavaScript
18
star
14

window-performance

A small JavaScript plugin to view the window.performance data
JavaScript
15
star
15

browser-diff

Get the differences between text files
JavaScript
15
star
16

xbl-analysis

HTML
13
star
17

codemirror-accessible

JavaScript
9
star
18

musicviz

A Canvas / Audio Experiment
JavaScript
9
star
19

wordsolver.js

Unscramble words in browser with a variety of dictionaries
JavaScript
8
star
20

inverse-intersection

Given a parent rectangle and a collection of children rectangles on a 2D plane, find a collection of non-overlapping rectangles that cover the inverse of the original set.
JavaScript
8
star
21

ColorStash

JavaScript
6
star
22

webdesigner

A totally unfinished visual web page editor...
JavaScript
5
star
23

photo-editor

JavaScript
5
star
24

neon-mentat

JavaScript
5
star
25

esm_sandbox_eval

JavaScript
4
star
26

arewefissionyet

JavaScript
3
star
27

firefox-patches

Patch queue for Firefox
3
star
28

bgrins.github.com

My Github Page
HTML
3
star
29

html5-workshop

Content to go along with ComoRichWeb workshop night for learning HTML5
3
star
30

sqlite-datagrid-demo

JavaScript
2
star
31

image-editor-scaffold

Experimental image editor project scaffold. Not ready for prime time in any way
JavaScript
2
star
32

firefox-user-testing-helpers

Batchfile
2
star
33

asciigram

JavaScript
2
star
34

data-ui-tests

HTML
2
star
35

table_to_csv

Convert HTML table to CSV
HTML
2
star
36

unicode_searcher

search for unicode characters
JavaScript
1
star
37

webdata

JavaScript
1
star
38

console-output-experiment

JavaScript
1
star
39

-super-spork

JavaScript
1
star
40

caption-o-matic

CSS
1
star
41

xul-document-burndown

JavaScript
1
star
42

component-prototype

JavaScript
1
star
43

bug-tools

Some scripts for working with bugzilla and commits to mozilla-central
JavaScript
1
star
44

comorichweb

JavaScript
1
star
45

colorwheel-full

1
star
46

rust-ci-starter

Rust
1
star
47

marquee-test

HTML
1
star
48

areweyet

Shared code for arewe*yet sites
JavaScript
1
star
49

Original_DrawWindow_Development

Render HTML in the Canvas Element
PHP
1
star
50

imgedit

Playing around with canvas... totally unfinished
JavaScript
1
star
51

ColorStashMini

A devtools playground for talks
JavaScript
1
star
52

opfs-sqlite-demo

JavaScript
1
star
53

kitchen-sink

JavaScript
1
star
54

webdev-sandbox

TypeScript
1
star
55

editor-tests

JavaScript
1
star
56

benchmark-reporting-server

JavaScript
1
star
57

crossbrowser-selenium-action-test

JavaScript
1
star
58

alt-text-test-results

HTML
1
star
59

compat-utils

JavaScript
1
star