Examples
Select a single city
$('#example-1').select3({
allowClear: true,
items: ['Amsterdam', 'Antwerp'/*, ...*/],
placeholder: 'No city selected'
});
Select multiple cities
$('#example-2').select3({
items: ['Amsterdam', 'Antwerp'/*, ...*/],
multiple: true,
placeholder: 'Type to search a city'
});
Select a city by country
$('#example-3').select3({
allowClear: true,
items: [{
text: 'Austria',
children: [ { id: 54, text: 'Vienna' } ]
} /*, ...*/],
placeholder: 'No city selected'
});
Select a city by timezone
$('#example-4').select3({
allowClear: true,
items: [{
id: '+00:00',
text: 'Western European Time Zone',
submenu: {
items: [
{ id: 4, text: 'Barcelona' }
/*, ...*/
],
showSearchInput: true
}
} /*, ...*/],
placeholder: 'No city selected',
showSearchInputInDropdown: false
});
Enter one or more email addresses
$('#example-5').select3({
inputType: 'Email',
placeholder: 'Type or paste email addresses'
});
Search a GitHub repository
Note: If this example stops working, please wait a few minutes. There is an active rate-limit in the GitHub API.
$('#example-6').select3({
ajax: {
url: 'https://api.github.com/search/repositories',
dataType: 'json',
minimumInputLength: 3,
quietMillis: 250,
params: function(term, offset) {
// GitHub uses 1-based pages with 30 results, by default
return { q: term, page: 1 + Math.floor(offset / 30) };
},
processItem: function(item) {
return {
id: item.id,
text: item.name,
description: item.description
};
},
results: function(data, offset) {
return {
results: data.items,
more: (offset + data.items.length > data.total_count)
};
}
},
placeholder: 'Search for a repository',
templates: {
resultItem: function(item) {
return (
'<div class="select3-result-item" data-item-id="' + item.id + '">' +
'<b>' + escape(item.text) + '</b><br>' +
escape(item.description) +
'</div>'
);
}
}
});
API
Call $(selector).select3(options)
to initialize a Select3 instance on the element
specified by the given selector. The element should be a <div>
element which
will be used as container to render the Select3 control in. If the element is a
<select>
element instead, this element will be used for initializing some of
the options below after which it is replaced with a <div>
element to contain
the Select3 control.
The following options are supported:
Option | Type | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ajax | Object |
This object is used for convenience for loading result items through AJAX requests
to a remote resource. Below is an overview of properties that may be set on this
object. Any other properties on this object are passed directly to the
transport, which by default is
$.ajax() .
|
||||||||||||||||||
allowClear | Boolean |
Set to true to allow the selection to be cleared. This option only
applies to single-value inputs.If you have used a <select> element to initialize the Select3
instance, this option defaults to true unless the element has a
required attribute. |
||||||||||||||||||
backspace |
Boolean |
If set to true , when the user enters a backspace while there is no text
in the search field but there are selected items, the last selected item will be
highlighted and when a second backspace is entered the item is deleted. If
false , the item gets deleted on the first backspace. The default value
is true on devices that have touch input and false on
devices that don't. |
||||||||||||||||||
closeOnSelect | Boolean |
Set to false to keep the dropdown open after the user has selected an
item. This is useful if you want to allow the user to quickly select multiple items.
The default value is true . |
||||||||||||||||||
createTokenItem | Function |
Function to create a new item from a user's search term. This is used to turn the
term into an item when dropdowns are disabled and the user presses Enter. It is also
used by the default tokenizer to create items for individual tokens. The function
receives a token parameter which is the search term (or part of a
search term) to create an item for and must return an item object with
id and text properties or null if no token
can be created from the term. The default is a function that returns an item where
the ID and text both match the term for any non-empty string and which returns
null otherwise.This option only applies to multiple-value inputs. |
||||||||||||||||||
data | Object or Array |
Initial selection data to set. This should be an object with id and
text properties in the case of input type 'Single', or an array of such
objects otherwise. This option is mutually exclusive with value . |
||||||||||||||||||
dropdown | Function |
Custom dropdown implementation to use for this instance. | ||||||||||||||||||
dropdownCssClass | String |
Optional CSS class to add to the dropdown's top-level element. | ||||||||||||||||||
initSelection | Function |
Function to map values by ID to selection data. This function receives two
arguments, value and callback . The value is the current
value of the selection, which is an ID or an array of IDs, depending on the input
type. The callback should be invoked with an object containing id and
text properties, or an array of such objects (again, depending on the
input type). |
||||||||||||||||||
inputType | String or Function |
The input type to use. Default input types include 'Multiple', 'Single' and 'Email',
but you can add custom input types to the Select3.InputTypes map or
just specify one here as a function. The default value is 'Single', unless
multiple is true in which case it is 'Multiple'. |
||||||||||||||||||
items | Array |
Array of items from which to select. Should be an array of objects with
id and text properties. As convenience, you may also pass
an array of strings, in which case the same string is used for both the ID and the
text. Items may be nested by adding a children property to any item,
whose value should be another array of items. Items that have children may omit
having an ID.If items are specified, all items are expected to be available locally
and all selection operations operate on this local array only. If omitted, items are
not available locally, and the query option should be provided to fetch
data.If you have used a <select> element to initialize the
Select3 instance, its <option> and <optgroup>
elements are used to initialize the items array. |
||||||||||||||||||
matcher | Function |
Function to determine whether text matches a given search term. Note this function
is only used if you have specified an array of items. Receives two arguments:
term and text . The term is the search term, which for
performance reasons has always been already processed using
Select3.transformText() . The text is the text that should match the
search term.The function should return true if the text matches the term, and
false otherwise. |
||||||||||||||||||
multiple | boolean |
May be set to true as short-hand for specifying
inputType: 'Multiple' .If you have used a <select> element to initialize the Select3
instance, this option is set to true when the element has a
multiple attribute. |
||||||||||||||||||
placeholder | String |
Placeholder text to display when the element has no focus and selected items. If you have used a <select> element to initialize the Select3
instance, this option may be set through the data-placeholder
attribute. |
||||||||||||||||||
positionDropdown | Function |
Function to position the dropdown. Receives $dropdownEl (the element to
be positioned) and $selectEl (the element of the Select3 instance) as
arguments. The default implementation positions the dropdown element under the
Select3 instance's element and gives it the same width as well. |
||||||||||||||||||
query | Function |
Function to use for querying items. Receives a single object with the following
properties:
items option is used. |
||||||||||||||||||
readOnly | Boolean |
If true , disables any modification of the input. |
||||||||||||||||||
removeOnly | Boolean |
If true , disables any modification of the input except removing of
selected items. |
||||||||||||||||||
showDropdown | Boolean |
Set to false if you don't want to use any dropdown (you can still open
it programmatically using open() ). |
||||||||||||||||||
show |
Boolean |
Set to false to remove the search input used in dropdowns. This option
only applies to single-value inputs, as multiple-value inputs don't have the search
input in the dropdown to begin with. The default is true . |
||||||||||||||||||
suppressMouse |
String or null |
The Select3 Dropdown by default suppresses mousewheel events so that any scrolling
in the dropdown doesn't affect the scroll position of the page. Through this option
you can select which selector should be monited for scroll events to suppress. Set
it to null to disable suppressing of mousewheel events altogether. The
default value is ".select3-results-container" . |
||||||||||||||||||
templates | Object |
Object with instance-specific templates to override the global templates assigned to
Select3.Templates . Inline documentation for the templates is provided
in select3-templates.js. |
||||||||||||||||||
tokenizer | Function |
Function for tokenizing search terms. Will receive the input (the input
string to tokenize), selection (the current selection data),
createToken (callback to create a token from the search terms, should
be passed an item object with id and text properties) and
options (the options set on the Select3 instance) arguments. Any string
returned by the tokenizer function is treated as the remainder of untokenized input.
This option only applies to multiple-value inputs. |
||||||||||||||||||
tokenSeparators | Array |
Array of string separators which are used to separate the search term into tokens.
If specified and the tokenizer property is not set, the tokenizer property will be
set to a function which splits the search term into tokens separated by any of the
given separators. The tokens will be converted into selectable items using the
createTokenItem function. The default tokenizer also filters out
already selected items. This option only applies to multiple-value inputs. |
||||||||||||||||||
value | ID or Array |
Initial value to set. This should be an ID (string or number) in the case of input
type 'Single', or array of IDs otherwise. This property is mutually exclusive with
data . |
Methods
Methods are invoked by passing the name of the method, followed by any arguments, to the
select3()
function. Example:
$(selector).select3('data', { id: 1, text: 'Amsterdam' })
add
(multiple-value inputs only)
Adds an item to the selection.
Argument | Type | Description |
---|---|---|
item | ID or Object |
The item to add, either as ID or object with id and text
properties. |
Examples:
$(selector).select3('add', 1)
$(selector).select3('add', { id: 1, text: 'Amsterdam' })
close
Closes the dropdown.
data
Sets or gets the data of the selection. If no argument is given, the current data is returned. Otherwise, the data is set to the argument given.
When new data is set, the appropriate change
event is automatically
generated, unless triggerChange
is set to false
.
Argument | Type | Description |
---|---|---|
data | Object or Array |
The new data to set, either as object with id and text
properties (for single-value inputs) or as array of such objects (for multiple-value
inputs). |
options | Object |
Optional options object. May contain the following property:
|
Examples:
// returns the current data:
$(selector).select3('data')
// sets new data on a single-value input:
$(selector).select3('data', { id: 1, text: 'Amsterdam' })
// sets new data on a multiple-value input:
$(selector).select3('data', [{ id: 1, text: 'Amsterdam' }, { id: 2, text: 'Antwerp' }])
focus
Focuses the search input.
open
Opens the dropdown.
remove
(multiple-value inputs only)
Removes an item from the selection.
Argument | Type | Description |
---|---|---|
item | ID or Object |
The item to remove, either as ID or object with id and
text properties. |
Examples:
$(selector).select3('remove', 1)
$(selector).select3('remove', { id: 1, text: 'Amsterdam' })
value / val
Sets or gets the value of the selection. If no argument is given, the current value is returned. Otherwise, the value is set to the argument given.
Note: When setting the value of the selection, the full data (including the
text
properties) is updated as well. In order to detect the right text for the
ID(s), Select3 will inspect the items
array to look up the corresponding item(s).
If no items have been provided, it is assumed the text should be equal to the ID. If this is
not what you want, you may want to pass the initSelection
option to the Select3
instance to override this process.
When a new value is set, the appropriate change
event is automatically
generated, unless triggerChange
is set to false
.
Argument | Type | Description |
---|---|---|
value | ID or Array |
The new value to set, either as single ID (for single-value inputs) or an array of IDs (for multiple-value inputs). |
options | Object |
Optional options object. May contain the following property:
|
Examples:
// returns the current value:
$(selector).select3('value')
$(selector).select3('val')
// sets a new value on a single-value input:
$(selector).select3('value', 1)
// sets a new value on a multiple-value input:
$(selector).select3('value', [1, 2])
Events
All of these events are emitted on the element to which the Select3 instance is attached and can
be listened to using jQuery's on()
method.
Note: If you have used a <select>
element for initializing the Select3
instance, you should realize it is replaced by a <div>
element after the
initialization. Any events will be triggered on the latter.
Event | Description |
---|---|
"change" |
Emitted when the selection has been changed. The event object will contain the
following properties:
|
"select3-closed" |
Emitted when the dropdown is closed. |
"select3-highlight" |
Emitted when an item in the dropdown is highlighted. The event object will contain
the following properties:
|
"select3-open" |
Emitted when the dropdown is opened. |
"select3-opening" |
Emitted when the dropdown is about to be opened. You can call
preventDefault() on the event object to cancel the opening of the
dropdown. |
"select3-selected" |
Emitted when an item has been selected. The event object will contain the following
properties:
|
"select3-selecting" |
Emitted when an item is about to be selected. You can call
preventDefault() on on the event object to prevent the item from being
selected. The event object will contain the following properties:
|