Add and remove items in any order without touching your mouse. Use
your left/right arrow keys to move the caret (ibeam) between items.
This example is instantiated from a <input type="text">
element (note that the value is represented as a string).
Show Code $('#input-tags' ).selectize({
delimiter: ',' ,
persist: false ,
create: function (input) {
return {
value: input,
text: input
}
}
});
Email:
Current Value: null
This demonstrates two main things: (1) custom item and option
rendering, and (2) item creation on-the-fly. Try typing a valid and
invalid email address.
Show Code var REGEX_EMAIL = '([a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@' +
'(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)' ;
$('#select-to' ).selectize({
theme: 'contacts' ,
persist: false ,
maxItems: null ,
valueField: 'email' ,
labelField: 'name' ,
searchField: ['name' , 'email' ],
options: [
{email: 'brian@thirdroute.com' , name: 'Brian Reavis' },
{email: 'nikola@tesla.com' , name: 'Nikola Tesla' },
{email: 'someone@gmail.com' }
],
render: {
item: function (item, escape) {
return '<div>' +
(item.name ? '<span class="name">' + escape(item.name) + '</span>' : '' ) +
(item.email ? '<span class="email">' + escape(item.email) + '</span>' : '' ) +
'</div>' ;
},
option: function (item, escape) {
var label = item.name || item.email;
var caption = item.name ? item.email : null ;
return '<div>' +
'<span class="label">' + escape(label) + '</span>' +
(caption ? '<span class="caption">' + escape(caption) + '</span>' : '' ) +
'</div>' ;
}
},
create: function (input) {
if ((new RegExp('^' + REGEX_EMAIL + '$' , 'i' )).test(input)) {
return {email: input};
}
var match = input.match(new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$' , 'i' ));
if (match) {
return {
email : match[2 ],
name : $.trim(match[1 ])
};
}
alert('Invalid email address.' );
return false ;
}
});
The most vanilla of examples.
Show Code $('#select-beast' ).selectize({
create: true ,
sortField: 'text'
});
Selectize supports <optgroup> rendering (as of v0.5.0).
Show Code $('#select-gear' ).selectize({
sortField: 'text'
});
States:
Current Value: ["CA","WY"]
This example only allows 3 items. Select one more item and the
control will be disabled until one or more are deleted.
Show Code $('#select-state' ).selectize({
maxItems: 3
});
Country:
Current Value: ""
A good example of (1) support for international characters
(diacritics) and (2) how items are scored and sorted. Try typing
"islands", for instance.
Show Code $('#select-country' ).selectize();
Repository:
Current Value: "https://github.com/brianreavis/selectize.js"
This demo shows how to integrate third-party data from the GitHub
API.
Show Code $('#select-repo' ).selectize({
theme: 'repositories' ,
valueField: 'url' ,
labelField: 'name' ,
searchField: 'name' ,
create: false ,
render: {
option: function (item, escape) {
return '<div>' +
'<span class="title">' +
'<span class="name"><i class="icon ' + (item.fork ? 'fork' : 'source' ) + '"></i>' + escape(item.name) + '</span>' +
'<span class="by">' + escape(item.username) + '</span>' +
'</span>' +
'<span class="description">' + escape(item.description) + '</span>' +
'<ul class="meta">' +
(item.language ? '<li class="language">' + escape(item.language) + '</li>' : '' ) +
'<li class="watchers"><span>' + escape(item.watchers) + '</span> watchers</li>' +
'<li class="forks"><span>' + escape(item.forks) + '</span> forks</li>' +
'</ul>' +
'</div>' ;
}
},
score: function (search) {
var score = this .getScoreFunction(search);
return function (item) {
return score(item) * (1 + Math.min(item.watchers / 100 , 1 ));
};
},
load: function (query, callback) {
if (!query.length) return callback();
$.ajax({
url: 'https://api.github.com/legacy/repos/search/' + encodeURIComponent(query),
type: 'GET' ,
error: function () {
callback();
},
success: function (res) {
callback(res.repositories.slice(0 , 10 ));
}
});
}
});
This demo shows how to integrate third-party data from the Rotten
Tomatoes API. Try searching for "Iron Man". Note:
if this doesn't work, it's because the API limit has been reached...
try again later :)
Show Code $('#select-movie' ).selectize({
theme: 'movies' ,
valueField: 'title' ,
labelField: 'title' ,
searchField: 'title' ,
options: [],
create: false ,
render: {
option: function (item, escape) {
var actors = [];
for (var i = 0 , n = item.abridged_cast.length; i < n; i++) {
actors.push('<span>' + escape(item.abridged_cast[i].name) + '</span>' );
}
return '<div>' +
'<img src="' + escape(item.posters.thumbnail) + '" alt="">' +
'<span class="title">' +
'<span class="name">' + escape(item.title) + '</span>' +
'</span>' +
'<span class="description">' + escape(item.synopsis || 'No synopsis available at this time.' ) + '</span>' +
'<span class="actors">' + (actors.length ? 'Starring ' + actors.join(', ' ) : 'Actors unavailable' ) + '</span>' +
'</div>' ;
}
},
load: function (query, callback) {
if (!query.length) return callback();
$.ajax({
url: 'http://api.rottentomatoes.com/api/public/v1.0/movies.json' ,
type: 'GET' ,
dataType: 'jsonp' ,
data: {
q: query,
page_limit: 10 ,
apikey: '3qqmdwbuswut94jv4eua3j85'
},
error: function () {
callback();
},
success: function (res) {
callback(res.movies);
}
});
}
});
State:
Current Value: ""
City:
Current Value: ""
A demonstration showing how to use the API to cascade controls for a
classic state / city selector. Note: The API for
fetching cities is a little spotty, so if it fails to list cities,
that's what's going on (try another state).
Show Code var xhr;
var select_state, $select_state;
var select_city, $select_city;
$select_state = $('#select-cities-state' ).selectize({
onChange: function (value) {
if (!value.length) return ;
select_city.disable();
select_city.clearOptions();
select_city.load(function (callback) {
xhr && xhr.abort();
xhr = $.ajax({
url: 'http://www.corsproxy.com/api.sba.gov/geodata/primary_city_links_for_state_of/' + value + '.json' ,
success: function (results) {
select_city.enable();
callback(results);
},
error: function () {
callback();
}
})
});
}
});
$select_city = $('#select-cities-city' ).selectize({
valueField: 'name' ,
labelField: 'name' ,
searchField: ['name' ]
});
select_city = $select_city[0 ].selectize;
select_state = $select_state[0 ].selectize;
select_city.disable();
Press the [backspace] key and go back to editing the item without it
being fully removed.
Show Code $('#input-tags2' ).selectize({
plugins: ['restore_on_backspace' ],
delimiter: ',' ,
persist: false ,
create: function (input) {
return {
value: input,
text: input
}
}
});
This plugin adds classic a classic remove button to each item for
behavior that mimics Select2 and Chosen.
Show Code $('#input-tags3' ).selectize({
plugins: ['remove_button' ],
delimiter: ',' ,
persist: false ,
create: function (input) {
return {
value: input,
text: input
}
}
});
Adds drag-and-drop support for easily rearranging selected items.
Requires jQuery UI (sortable).
Show Code $('#input-draggable' ).selectize({
plugins: ['drag_drop' ],
delimiter: ',' ,
persist: false ,
create: function (input) {
return {
value: input,
text: input
}
}
});
A plugin by Simon Hewitt that renders optgroups horizontally with convenient
left/right keyboard navigation.
Show Code $("#select-car" ).selectize({
options: [
{id: 'avenger' , make: 'dodge' , model: 'Avenger' },
{id: 'caliber' , make: 'dodge' , model: 'Caliber' },
{id: 'caravan-grand-passenger' , make: 'dodge' , model: 'Caravan Grand Passenger' },
{id: 'challenger' , make: 'dodge' , model: 'Challenger' },
{id: 'ram-1500' , make: 'dodge' , model: 'Ram 1500' },
{id: 'viper' , make: 'dodge' , model: 'Viper' },
{id: 'a3' , make: 'audi' , model: 'A3' },
{id: 'a6' , make: 'audi' , model: 'A6' },
{id: 'r8' , make: 'audi' , model: 'R8' },
{id: 'rs-4' , make: 'audi' , model: 'RS 4' },
{id: 's4' , make: 'audi' , model: 'S4' },
{id: 's8' , make: 'audi' , model: 'S8' },
{id: 'tt' , make: 'audi' , model: 'TT' },
{id: 'avalanche' , make: 'chevrolet' , model: 'Avalanche' },
{id: 'aveo' , make: 'chevrolet' , model: 'Aveo' },
{id: 'cobalt' , make: 'chevrolet' , model: 'Cobalt' },
{id: 'silverado' , make: 'chevrolet' , model: 'Silverado' },
{id: 'suburban' , make: 'chevrolet' , model: 'Suburban' },
{id: 'tahoe' , make: 'chevrolet' , model: 'Tahoe' },
{id: 'trail-blazer' , make: 'chevrolet' , model: 'TrailBlazer' },
],
optgroups: [
{id: 'dodge' , name: 'Dodge' },
{id: 'audi' , name: 'Audi' },
{id: 'chevrolet' , name: 'Chevrolet' }
],
labelField: 'model' ,
valueField: 'id' ,
optgroupField: 'make' ,
optgroupLabelField: 'name' ,
optgroupValueField: 'id' ,
optgroupOrder: ['chevrolet' , 'dodge' , 'audi' ],
searchField: ['model' ],
plugins: ['optgroup_columns' ]
});