Create elegant taggable fields with copy/paste and keyboard support.
<input type="text" value="red,green,blue">
$('#tokenfield-1').tokenfield()
$('.tokenfield').tokenfield()
Name | type | default | description |
---|---|---|---|
tokens | string, array | [ ] | Tokens (or tags). Can be a string with comma-separated values ("one,two,three" ), an array of strings (["one","two","three"] ), or an array of objects ([{ value: "one", label: "Einz" }, { value: "two", label: "Zwei" }] ) |
allowDuplicates | boolean | false | Whether to allow duplicate tokens or not. A token is considered a duplicated if it's value matches an existing token's value. Therefore, it is possible to enter multiple tokens with the same label, if their values are diefferent. |
minLength | int | 0 | Minimum length required for token value. |
minWidth | int | 60 | Minimum input field width. In pixels. |
autocomplete | object | {} | jQuery UI Autocomplete options |
showAutocompleteOnFocus | boolean | false | Whether to show autocomplete suggestions menu on focus or not. |
Initializes an input with a tokenfield.
$('#myField').tokenfield();
Manually set the tokenfield content (replacing the old content)
$('#myField').tokenfield('setTokens', 'blue,red,white'); $('#myField').tokenfield('setTokens', ['blue','red','white']); $('#myField').tokenfield('setTokens', [{ value: "blue", label: "Blau" }, { value: "red", label: "Rot" }]);
Manually create a token and append it to the input
$('#myField').tokenfield('createToken', 'purple'); $('#myField').tokenfield('createToken', { value: 'violet', label: 'Violet' });
Get an array of tokens from the input
$('#myField').tokenfield('getTokens');
Get a comma-separated list of the tokens from the input
$('#myField').tokenfield('getTokensList');
Tokenfield exposes a few events for hooking into it's functionality.
Event | Description |
---|---|
beforeCreateToken | This event fires when a token is all set up to be created, but before it is inserted into the DOM and event listeners are attached. You can use this event to manipulate token value and label by changing the appropriate values of token property of the event. See below for an example. |
afterCreateToken | This event is fired after the token has been created. Here, token property of the event is also available, but is basically read-only. |
beforeEditToken | This event is fired just before a token is about to be edited. This allows you to manipluate the input field value before it is created. Again, to do this, manipluate the token property of the event. |
removeToken | This event is fired after a token is removed. |
$('#myTokenfield').on('beforeCreateToken', function (e) { e.token.label = 'Something else' }) $('#myTokenfield').on('afterCreateToken', function (e) { e.token // token object with value and label }) $('#myTokenfield').on('beforeEditToken', function (e) { e.token.value = 'Edit this instead' }) $('#myTokenfield').on('removeToken', function () { alert('Token removed!') })
Tokenfield includes support for manipulating tokens via keyboard
Arrow keys will move between active tokens. Try it out: click on one of the tokens and press left and right arrow keys
You can delete a selected token with backspace or delete keys. Try it out now:
If You have one token selected, you can select all tokens with the keyboard. Then, you can copy the tokens using keyboard. You can also paste tokens to another field.
You can copy tokens from a tokenfield and paste them to any other field as comma-separated values. When you paste to another tokenfield, they will become tokens there, aswell!
Try it out, copy the following to the field below: violet,yellow,brown
Tokenfield also supports all the default validation states from Bootstrap