Download

NPM

You can also get the latest release using NPM. This release contains source files as well as the compiled CSS and JavaScript files.

npm install materialize-tags

Examples

1. Basic example without preloaded tags

Just add data-role="materialtags" to your input field to automatically change it to a tags input field.

statement returns
$("input").val()
$("input").materialtags('items')

2. Basic example with preloaded tags

Just add data-role="materialtags" to your input field to automatically change it to a tags input field.

statement returns
$("input").val()
$("input").materialtags('items')

3. Using TypeAhead with Materialize-Tags

Typeahead is not included in MaterializeCSS and Materialize Tags. Therefore, if you which to use the functionality offered by TypeAhead, you will have to include your own typeahead library.
The original version of typeahead.js was developed by twitter, unfortunately this project is unmaintained for more than two years. Thus, we have choosen to use the forked project by the CoreJavascript Team: Go to Github Repository.

All functions works seamlessly with the latest version: v1.2.1.
Warning: New functionalities (e.g. autoselect) are only available using TypeAhead 1.2.0 or more recent.

The latest release file can be download from this link: go to GitHub

3.1. Selecting values with TypeAhead - Basic Version

Typeahead allows you to propose a list of valid tags that the user can choose. Very useful to recommend a list of values to user or restrict him to certain values.

Here is an example below:

statement returns
$("input").val()
$("input").materialtags('items')
3.2. Selecting values with TypeAhead - Pattern Highlight

In addition to proposing a list of values, Typeahead can highlight the matching pattern in the list of recommendation displayed.

Here is an example below:

statement returns
$("input").val()
$("input").materialtags('items')
3.3. Selecting values with TypeAhead - Pattern Highlight and AutoSelect

In addition to proposing a list of values and highlight the matching pattern, Typeahead can automatically select the first proposition.

Here is an example below:

statement returns
$("input").val()
$("input").materialtags('items')
4. Objects as tags

Instead of just adding strings as tags, bind objects to your tags. This makes it possible to set id values in your input field's value, instead of just the tag's text.

statement returns
$("input").val()
$("input").materialtags('items')
option description
tagClass

Classname for the tags, or a function returning a classname

$('input').materialtags({
        tagClass: 'big'
    });
$('input').materialtags({
        tagClass: function(item) {
            return (item.length > 10 ? 'big' : 'small');
        }
    });
itemValue

When adding objects as tags, itemValue must be set to the name of the property containing the item's value, or a function returning an item's value.

$('input').materialtags({
        itemValue: 'id'
    });
$('input').materialtags({
        itemValue: function(item) {
            return item.id;
        }
    });
itemText

When adding objects as tags, you can set itemText to the name of the property of item to use for a its tag's text. You may also provide a function which returns an item's value. When this options is not set, the value of itemValue will be used.

$('input').materialtags({
        itemText: 'label'
    });
$('input').materialtags({
        itemText: function(item) {
            return item.label;
        }
    });
confirmKeys

Array of keycodes which will add a tag when typing in the input. (default: [13, 188], which are ENTER and comma)

$('input').materialtags({
        confirmKeys: [13, 44]
    });
maxTags

When set, no more than the given number of tags are allowed to add (default: undefined). When maxTags is reached, a class 'bootstrap-materialtags-max' is placed on the materialtags element.

$('input').materialtags({
        maxTags: 3
    });
maxChars

Defines the maximum length of a single tag. (default: undefined)

$('input').materialtags({
        maxChars: 8
    });
trimValue

When true, automatically removes all whitespace around tags. (default: false)

$('input').materialtags({
        trimValue: true
    });
allowDuplicates

When true, the same tag can be added multiple times. (default: false)

$('input').materialtags({
        allowDuplicates: true
    });
allowTabOnEmpty

When true, the behavior of tab key is not overridden only if no text is entered after create tags or when the input has no tags (default: false)

$('input').materialtags({
        allowTabOnEmpty: true
    })
freeInput

Allow creating tags which are not returned by typeahead's source (default: true)

This is only possible when using string as tags. When itemValue option is set, this option will be ignored.
$('input').materialtags({
        typeahead: {
            source: ['Amsterdam', 'Washington', 'Sydney', 'Beijing', 'Cairo']
        },
        freeInput: true
    });
typeahead

Object containing typeahead specific options

source

An array (or function returning a promise or array), which will be used as source for a typeahead.

$('input').materialtags({
        typeahead: {
            source: ['Amsterdam', 'Washington', 'Sydney', 'Beijing', 'Cairo']
        }
    });
$('input').materialtags({
        typeahead: {
            source: function(query) {
                return $.get('http://someservice.com');
            }
        }
    });
cancelConfirmKeysOnEmpty

Boolean value controlling whether form submissions get processed when pressing enter in a field converted to a materialtags (default: false).

$('input').materialtags({
        cancelConfirmKeysOnEmpty: true
    });
onTagExists

Function invoked when trying to add an item which allready exists. By default, the existing tag hides and fades in.

$('input').materialtags({
        onTagExists: function(item, $tag) {
            $tag.hide().fadeIn();
        }
    });
method description
add

Adds a tag

$('input').materialtags('add', 'some tag');
$('input').materialtags('add', { id: 1, text: 'some tag' });
Optionally, you can pass a 3rd parameter (object or value) to the add method to gain more control over the process. The parameter is exposed in the options attribute of the event.
$('input').materialtags('add', 'some tag', {preventPost: true});
Usage:
$('#tags-input').on('beforeItemAdd', function(event) {
        var tag = event.item;
        // Do some processing here
        if (!event.options || !event.options.preventPost) {
            $.ajax('/ajax-url', ajaxData, function(response) {
                if (response.failure) {
                    // Remove the tag since there was a failure
                    // "preventPost" here will stop this ajax call from running when the tag is removed
                    $('#tags-input').materialtags('remove', tag, {preventPost: true});
                }
            });
        }
    });
remove

Removes a tag

$('input').materialtags('remove', 'some tag');
$('input').materialtags('remove', { id: 1, text: 'some tag' });
Optionally, you can pass a 3rd parameter (object or value) to the remove method to gain more control over the process. The parameter is exposed in the options attribute of the event.
$('input').materialtags('remove', 'some tag', {preventPost: true});
Usage:
$('#tags-input').on('beforeItemRemove', function(event) {
        var tag = event.item;
        // Do some processing here
        if (!event.options || !event.options.preventPost) {
            $.ajax('/ajax-url', ajaxData, function(response) {
                if (response.failure) {
                    // Re-add the tag since there was a failure
                    // "preventPost" here will stop this ajax call from running when the tag is added
                    $('#tags-input').materialtags('add', tag, {preventPost: true});
                }
            });
        }
    });
removeAll

Removes all tags

$('input').materialtags('removeAll');
focus

Sets focus in the materialtags

$('input').materialtags('focus');
input

Returns the materialtags's internal <input />, which is used for adding tags. You could use this to add your own typeahead behaviour for example.

var $elt = $('input').materialtags('input');
refresh

Refreshes the tags input UI. This might be usefull when you're adding objects as tags. When an object's text changes, you'll have to refresh to update the matching tag's text.

$('input').materialtags('refresh');
destroy

Removes materialtags behaviour

$('input').materialtags('destroy');
event description
itemAddedOnInit During initialization, pre-defined tags being added will cause this event to be triggered. Example:
$('input').on('itemAddedOnInit', function(event) {
        // event.item: contains the item
    });
beforeItemAdd Triggered just before an item gets added. Example:
$('input').on('beforeItemAdd', function(event) {
        // event.item: contains the item
        // event.cancel: set to true to prevent the item getting added
    });
itemAdded Triggered just after an item got added. Example:
$('input').on('itemAdded', function(event) {
        // event.item: contains the item
    });
beforeItemRemove Triggered just before an item gets removed. Example:
$('input').on('beforeItemRemove', function(event) {
        // event.item: contains the item
        // event.cancel: set to true to prevent the item getting removed
    });
itemRemoved Triggered just after an item got removed. Example:
$('input').on('itemRemoved', function(event) {
        // event.item: contains the item
    });