Selectize is a jQuery-based library that provides a highly customizable and feature-rich replacement for standard HTML select elements. It combines the best features of a text input, a dropdown menu, and a tagging interface, allowing users to easily select single or multiple values from a pre-defined list or even input new values. This makes it ideal for situations requiring efficient and user-friendly selection processes, particularly when dealing with large datasets or complex filtering needs. It’s designed to be lightweight, performant, and highly adaptable to various design aesthetics and usage scenarios.
Selectize depends on jQuery. Ensure you have jQuery included in your project before including Selectize. You can include Selectize via CDN or by downloading the library.
1. Using a CDN:
Include the jQuery library and the Selectize JavaScript and CSS files in the <head>
of your HTML document:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/css/selectize.bootstrap3.min.css" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/js/standalone/selectize.min.js"></script>
Note: Replace "https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/..."
with the correct CDN links if a different version is required. You may also choose a different CSS theme (e.g., selectize.default.css
).
2. Downloading the Library:
Download the Selectize library from the official repository (check the latest version) and include the CSS and JS files in your project. Ensure the paths are correct.
3. Basic Initialization:
Once included, initialize Selectize on your HTML <select>
element:
<select id="my-selectize">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<script>
$('#my-selectize').selectize();
</script>
This will replace the standard select element with the Selectize control. For more advanced configuration options, refer to the options section in the advanced usage guide.
The simplest way to create a Selectize instance is to apply the selectize()
method to a standard HTML <select>
element. Selectize will automatically detect the options within the <select>
and create the interactive control.
<select id="my-selectize">
<option value="A">Option A</option>
<option value="B">Option B</option>
<option value="C">Option C</option>
</select>
<script>
$('#my-selectize').selectize();
</script>
This will replace the <select>
element with a Selectize control containing the listed options. The id
attribute is crucial for selecting the element with jQuery. If you don’t have a pre-existing <select>
, you can also create a Selectize instance using a different approach (see the advanced usage section for details).
Selectize offers many configuration options to customize its behavior. These are passed as a JavaScript object to the selectize()
method. Here are a few fundamental options:
placeholder
: Sets placeholder text displayed in the input field when no item is selected.$('#my-selectize').selectize({
placeholder: 'Select an option'
; })
maxItems
: Limits the number of items that can be selected (for multiple selections). Set to 1
for single selections.$('#my-selectize').selectize({
maxItems: 1
; })
valueField
: Specifies the attribute used for the value of each selected item (defaults to 'value'
). Useful when your options have different value and text attributes.$('#my-selectize').selectize({
valueField: 'id',
labelField: 'name' // For the display text
; })
searchField
: Specifies which fields are searched for when a user types in the input field (supports array of fields).$('#my-selectize').selectize({
searchField: ['name', 'description']
; })
options
: Allows you to directly specify the options array instead of using a <select>
element.$('#my-selectize').selectize({
options: [
id: 'A', name: 'Option A'},
{id: 'B', name: 'Option B'}
{,
]valueField: 'id',
labelField: 'name'
; })
Adding items: Programmatically add items using the addItem()
method. This accepts the value
of the item.
var selectize = $('#my-selectize')[0].selectize; // Get the Selectize instance
.addItem('New Item Value', true); // true for adding without triggering change event selectize
Removing items: Use the removeItem()
method to remove an item by its value.
.removeItem('A'); selectize
To get the currently selected values:
var selectedValues = selectize.getValue(); // Returns an array of values
To get the currently selected text:
var selectedItems = selectize.items; // Returns an array of item objects (if valueField and labelField are used).
var selectedTexts = selectize.getValue(); // Returns array of values if labelField and valueField are not used. Or you can loop through selectedItems to extract the labelField values.
To set the selected values:
.setValue(['A', 'B']); // Sets the values - will automatically select corresponding items selectize
Selectize triggers several events that you can listen for. Here are a few common ones:
change
: Triggered when the selection changes.$('#my-selectize').on('change', function(e) {
console.log('Selected values:', e.target.value); // or use selectize.getValue() if needed
; })
item_add
: Triggered when an item is added (e.g., through user input or addItem()
).$('#my-selectize').on('item_add', function(e) {
console.log('Item added:', e.added);
; })
item_remove
: Triggered when an item is removed.$('#my-selectize').on('item_remove', function(e) {
console.log('Item removed:', e.removed);
; })
Remember to always get the Selectize instance using $('#my-selectize')[0].selectize
before calling its methods if you’re working with jQuery events or performing other actions directly on the Selectize object. Refer to the official documentation for a complete list of available events and options.
Selectize excels at handling data from various sources. Beyond directly specifying options in the HTML or the options
configuration, you can load data dynamically using the load
option. This option takes a function that receives a query string as an argument and is expected to return a promise or a jQuery AJAX object that resolves with an array of options.
$('#my-selectize').selectize({
valueField: 'id',
labelField: 'text',
searchField: ['text'],
load: function(query, callback) {
if (!query.length) return callback();
.ajax({
$url: '/api/search?q=' + query,
type: 'GET',
error: function() {
callback();
,
}success: function(res) {
callback(res);
};
})
}; })
This example fetches data from an API endpoint based on user input. The callback
function receives the array of options returned by the API. Error handling is crucial for a robust implementation. Consider using fetch
API for more modern approaches.
Fine-tune the search and filtering behavior using several options:
score
Function: Customize how search results are scored using a custom scoring function. This allows for more sophisticated matching logic.: function(search, option) {
scorevar score = 0;
if (option.text.toLowerCase().indexOf(search.toLowerCase()) !== -1) {
+= 10;
score
}if (option.description && option.description.toLowerCase().indexOf(search.toLowerCase()) !== -1) {
+= 5;
score
}return score;
}
diacritics
: Enables diacritic-insensitive searching (e.g., treating “é” and “e” as the same).: true diacritics
sortField
: Specifies a field to sort results by.: 'text' sortField
create
: Allows users to create new items not present in the data source. Set to a boolean (true
to enable), or a function for custom creation logic.: true // or a function to handle custom creation create
Customize the appearance of items in the dropdown and selected items using the render
options:
render
Functions: The render
option provides functions to customize the rendering of items in the dropdown (item
), the selected item (option
), and the input field (item
), and even the dropdown (dropdown
).: {
renderitem: function(item, escape) {
return '<div>' + escape(item.text) + ' - ' + escape(item.description) + '</div>';
,
}option: function(item, escape) {
return '<div>' + escape(item.text) + '</div>';
}, }
These functions receive the item data and an escape
function to prevent XSS vulnerabilities.
Selectize supports plugins that extend its functionality. Plugins are typically included via the plugins
option.
: ['remove_button'] // Example plugins
Creating a custom plugin involves defining a function that receives the Selectize instance as an argument and adds desired functionality. This is typically done by adding methods or modifying existing behavior. The documentation provides a clear guide on structuring custom plugins.
While Selectize doesn’t have built-in internationalization, you can achieve localization by customizing the text displayed through the render
functions and other options, potentially using a library like moment.js for date/time formatting. Consider using i18next
or similar for managing translations.
For accessibility, ensure sufficient contrast between text and background colors. Use descriptive labels and placeholder text. Proper ARIA attributes are essential for screen readers. Thoroughly test with assistive technologies to ensure full usability. Consider using ARIA attributes like aria-label
and aria-describedby
to provide more context. Keyboard navigation should be fully functional.
Selectize accepts data in several formats:
$('#my-selectize').selectize({
options: ['Option A', 'Option B', 'Option C']
; })
value
and a text
property.$('#my-selectize').selectize({
options: [
value: 'A', text: 'Option A'},
{value: 'B', text: 'Option B'},
{value: 'C', text: 'Option C'}
{,
]valueField: 'value',
labelField: 'text'
; })
Fetching data from remote sources is a common use case. Use the load
option to define a function that fetches data asynchronously. This function receives a query string (if applicable) and a callback function.
$('#my-selectize').selectize({
valueField: 'id',
labelField: 'name',
load: function(query, callback) {
fetch('/api/data?q=' + query)
.then(response => response.json())
.then(data => callback(data))
.catch(error => console.error('Error loading data:', error));
}; })
This example uses the fetch
API. Remember to handle errors appropriately and provide feedback to the user if the data loading fails. You can also use jQuery’s $.ajax
for similar functionality.
Initial loading: For static data, define the options
array during initialization. For dynamic data, use the load
option.
Updating data: To update the options dynamically after initialization, use the refreshItems()
method on the Selectize instance. This will re-render the dropdown with the updated data.
const selectize = $('#my-selectize')[0].selectize;
const newData = [{value: 'D', text: 'Option D'}];
.addOption(newData); //adds a single item
selectize.refreshItems(); // refresh the dropdown selectize
addOption(item)
and removeOption(value)
methods for adding and removing options respectively. refreshItems()
isn’t strictly necessary for these methods, but is good practice.Use events to respond to data changes:
change
: Triggered whenever the selection changes.
item_add
: Triggered when an item is added to the selection.
item_remove
: Triggered when an item is removed from the selection.
load
event: Handle events related to the load option - load
is triggered on data load start, load_end
on data load finish, and load_error
if there’s an error.
Listen to these events to update your application state accordingly, perform additional actions based on the user’s selections, or handle errors during data loading.
Selectize itself doesn’t provide built-in data validation. You’ll need to implement validation separately. This usually involves checking the data received from the server or the user input against your defined rules. You can use the change
event to trigger validation after a selection is made. Display error messages using appropriate UI elements. Consider using a dedicated validation library if you require complex validation rules.
Selectize’s appearance is highly customizable using CSS. The library provides a well-structured CSS framework, making it easy to target specific elements and modify their styles. The main container element has the class selectize-control
. Within this, you’ll find classes for various components such as the input field, dropdown, and individual items. Inspect the rendered HTML using your browser’s developer tools to identify the relevant classes for the specific elements you want to style.
For example, to change the background color of the input field, you could use:
.selectize-input {
background-color: #f0f0f0;
}
To change the color of the dropdown items:
.selectize-dropdown .selectize-dropdown-content li {
background-color: #e0e0e0;
color: #333;
}
Remember to be mindful of specificity when writing your CSS to avoid unintended style overrides.
Selectize integrates well with popular CSS frameworks like Bootstrap. Pre-built CSS files are often available to easily incorporate Selectize into your existing framework-based design. For example, if you’re using Bootstrap, include the appropriate Bootstrap-themed CSS file (e.g., selectize.bootstrap3.css
). This will automatically apply the framework’s styling to the Selectize control.
You can extensively customize the dropdown appearance. The render.item
function (see Advanced Configuration) allows you to control the HTML structure and styling of each item within the dropdown. You can use this to add icons, images, or custom formatting. Remember to escape any user-provided data within the render
function to prevent XSS vulnerabilities. Additionally, CSS can be used to style the overall dropdown, including its height, width, and scrolling behavior. Targeting the .selectize-dropdown
class and its children provides granular control over the dropdown’s style.
The input field’s appearance can be modified using CSS targeting the .selectize-input
class. You can adjust its size, padding, border, background color, and other properties. The placeholder text can be controlled via the placeholder
configuration option. For more advanced customizations related to the input’s behavior (e.g., adding icons), consider using custom rendering functions or even creating a custom plugin. You might also need to adjust the styling of the .selectize-input input
(to target the actual input element) to override default styles and apply specific styles directly to the input text area.
Selectize provides a rich set of events that allow you to respond to various user interactions and internal state changes. Here’s a summary of key events:
change
: Fired when the selected items change. This is a fundamental event for tracking user selections.
item_add
: Fired when a new item is added to the selection. Provides details about the added item.
item_remove
: Fired when an item is removed from the selection. Provides details about the removed item.
dropdown_open
: Fired when the dropdown is opened.
dropdown_close
: Fired when the dropdown is closed.
type
: Fired when the user types in the input field. Useful for implementing custom filtering or autocomplete logic.
blur
: Fired when the input field loses focus.
focus
: Fired when the input field gains focus.
load
: Fired when data loading begins (if using the load
option).
load_end
: Fired when data loading completes successfully.
load_error
: Fired if there is an error during data loading.
create
: Fired when a new item is created (if the create
option is enabled).
initialize
: Fired after the Selectize instance is fully initialized.
destroy
: Fired before the Selectize instance is destroyed.
This list isn’t exhaustive; check the latest Selectize documentation for the complete list and details on the data provided in each event’s event object.
The change
, item_add
, and item_remove
events are crucial for handling item selection and deselection. Use these events to update your application’s state, make API calls, or perform other actions based on the user’s choices.
$('#my-selectize').on('item_add', function(e) {
console.log('Item added:', e.added); // e.added contains the added item's value
// Perform actions based on the added item
;
})
$('#my-selectize').on('item_remove', function(e) {
console.log('Item removed:', e.removed); // e.removed contains the removed item's value
// Perform actions based on the removed item
; })
The type
event is triggered whenever the user types in the input field. You can leverage this event to implement custom filtering logic, display suggestions, or fetch data from a remote source based on the user’s input.
$('#my-selectize').on('type', function(e) {
const query = e.target.value;
console.log('User typed:', query);
// Implement your custom search logic here
; })
You can customize event handling by using event handlers as shown above (using jQuery’s on
method). Remember that many events provide additional data within the event object (e.g., e.added
in item_add
). You can also use the off
method to remove event handlers if necessary. For more complex scenarios, you might consider creating a custom plugin (as described in the Advanced Configuration section) to manage events and related functionality. Remember to retrieve the Selectize instance using $('#my-selectize')[0].selectize
if you’re working with jQuery events or performing other actions directly on the Selectize object.
Selectize not initializing: Double-check that jQuery is included before Selectize and that you’re correctly selecting the target <select>
element using its ID. Ensure the Selectize JavaScript and CSS files are correctly linked. Check your browser’s developer console for JavaScript errors.
Incorrect data display: Verify that the valueField
and labelField
options (if used) are correctly configured to match the structure of your data. Ensure your data is in the expected format (array of strings or array of objects).
Styling issues: Inspect the rendered HTML using your browser’s developer tools to identify the correct CSS classes to target. Ensure that your custom CSS is correctly applied and doesn’t conflict with Selectize’s default styles. Check for specificity conflicts in your CSS.
No results from remote data source: Verify that your server-side code is correctly handling requests and returning data in the expected JSON format. Check for network errors using your browser’s developer tools. Ensure your API endpoint is correctly configured and accessible.
Events not firing: Ensure you’re using the correct event names and that the event handler is correctly attached. Check the browser’s developer console for errors.
Browser developer tools: Use your browser’s developer tools (usually accessed by pressing F12) to inspect the HTML structure, debug JavaScript code, and examine network requests. The console will often display errors that help pinpoint the problem.
Console logging: Add console.log
statements to your JavaScript code to track variable values, function execution, and event triggers. This helps understand the flow of your code and identify potential issues.
Simplified test cases: Create minimal, reproducible examples to isolate the problem. Start with a basic Selectize initialization and gradually add features to identify the specific component causing the issue.
Inspect the Selectize instance: Access the Selectize instance using $('#my-selectize')[0].selectize
and inspect its properties and methods using the browser’s debugger to check the internal state of the control.
Optimize data loading: For large datasets, consider implementing pagination or filtering on the server-side to reduce the amount of data transferred to the client. Use efficient data structures and algorithms for client-side processing.
Minimize DOM manipulation: Avoid unnecessary DOM manipulations within the render
functions. Pre-render as much as possible outside the rendering callbacks to improve performance.
Use a lightweight theme: Avoid overly complex or resource-intensive CSS styles that could impact performance.
Lazy loading: If feasible, load data only when it’s needed.
jQuery version: Ensure you’re using a compatible version of jQuery. Check the Selectize documentation for supported jQuery versions.
Browser compatibility: While Selectize generally supports modern browsers, older browsers might have limited support. Thoroughly test your application across different browsers and versions.
Plugin compatibility: If using plugins, check for compatibility with the version of Selectize you’re using.
CSS framework compatibility: If using a CSS framework (like Bootstrap), ensure that its styles don’t conflict with Selectize’s styles. Use appropriate specificity in your CSS to override styles as needed.
This example demonstrates a basic Selectize implementation with a few options configured:
<!DOCTYPE html>
<html>
<head>
<title>Selectize Basic Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/css/selectize.bootstrap3.min.css" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/js/standalone/selectize.min.js"></script>
</head>
<body>
<select id="basic-selectize">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Orange</option>
</select>
<script>
$('#basic-selectize').selectize({
placeholder: 'Select a fruit',
maxItems: 2 // Allow selecting up to 2 fruits
;
})</script>
</body>
</html>
This code creates a simple Selectize control that allows selecting up to two fruits from a predefined list. The placeholder text guides the user.
This example demonstrates remote data loading and custom rendering:
$('#advanced-selectize').selectize({
valueField: 'id',
labelField: 'name',
searchField: ['name', 'description'],
create: true, // Allow creating new items
load: function(query, callback) {
.ajax({
$url: '/api/items?q=' + query,
type: 'GET',
dataType: 'json',
success: function(results) {
callback(results);
};
}),
}render: {
item: function(item, escape) {
return '<div>' + escape(item.name) + ' - ' + escape(item.description) + '</div>';
}
}; })
This code fetches data from an API endpoint (/api/items
), allows users to create new items, and customizes the rendering of items in the dropdown to include both name and description. Remember to replace /api/items
with your actual API endpoint.
Tagging systems: Selectize’s tagging capabilities make it ideal for applications requiring users to add tags or keywords. Examples include social media platforms, blog post editors, or content management systems.
Form inputs: Enhance standard select inputs in forms to provide a more user-friendly experience, especially for large lists of options or when multiple selections are required.
Search filters: Create sophisticated search filters that allow users to select multiple criteria to refine their search results. The autocomplete feature enhances the search experience.
Autocomplete suggestions: Improve the user experience of text inputs by providing intelligent autocomplete suggestions based on a pre-defined list or a remote data source.
Multi-select list boxes: Use Selectize to build user interfaces that require selecting multiple items from a list, such as assigning users to a project or choosing multiple categories for a product.
Remember to adapt these examples and use cases to your specific needs and data structures. Consider the available options and events to customize Selectize’s behavior fully.
This section provides a reference for the Selectize API. Due to the extensive nature of the API, this is a simplified overview. Consult the official Selectize documentation for the most complete and up-to-date information.
The Selectize constructor is invoked by calling selectize()
on a jQuery-wrapped <select>
element. Options are passed as a JavaScript object to customize the control’s behavior.
$('#my-selectize').selectize({
// options here
; })
The constructor returns a Selectize instance, which is an object representing the initialized control. This instance provides access to various methods and properties.
The Selectize instance exposes several methods to interact with the control programmatically:
addItem(value, silent)
: Adds an item to the selection. silent
(boolean) prevents triggering the change
event.
removeItem(value)
: Removes an item from the selection.
getValue()
: Returns an array of selected values.
setValue(values)
: Sets the selected values.
clear()
: Clears the selection.
destroy()
: Destroys the Selectize instance and restores the original <select>
element.
addOption(item)
: Adds a single option to the options list.
removeOption(value)
: Removes an option from the options list.
refreshItems()
: Re-renders the dropdown items.
focus()
: Sets focus to the input field.
blur()
: Removes focus from the input field.
isDisabled()
: Checks if the Selectize instance is disabled.
enable()
: Enables the Selectize instance.
disable()
: Disables the Selectize instance.
isOpen()
: Checks if the dropdown is open.
open()
: Opens the dropdown.
close()
: Closes the dropdown.
This is not an exhaustive list; refer to the official documentation for a complete list of methods and their parameters.
Selectize offers numerous options to customize its appearance and behavior. Some key options include:
placeholder
: Placeholder text displayed in the input field.
maxItems
: Maximum number of items that can be selected.
valueField
and labelField
: Specifies the attributes used for the value and label of each item, respectively. Essential when using objects as options.
options
: An array of options to be pre-populated in the control. This can be an array of strings or an array of objects with value
and text
properties.
load
: A function to load options from a remote data source.
create
: Enables creating new items that are not in the existing options list.
plugins
: An array of plugin names to enable.
render
: Object containing functions to customize rendering of items in the dropdown and selected items. Includes item
, option
, etc.
searchField
: Specifies the fields to be searched when a user types in the input.
sortField
: Specifies a field to sort items by.
The full list of options and their functionalities is available in the official documentation.
Selectize triggers a wide variety of events, allowing you to respond to various actions and changes in the control’s state. Some important events are:
change
: Fired when the selected items change.
item_add
: Fired when an item is added.
item_remove
: Fired when an item is removed.
dropdown_open
: Fired when the dropdown is opened.
dropdown_close
: Fired when the dropdown is closed.
type
: Fired when the user types in the input field.
load
: Fired when data loading starts (for remote data sources).
load_end
: Fired when data loading finishes successfully.
load_error
: Fired when there is an error during data loading.
create
: Fired when a new item is created (if create
is enabled).
initialize
: Fired after initialization.
destroy
: Fired before destruction.
Refer to the official documentation for a complete list of events and the details of the data they provide. Remember to use on()
and off()
to attach and detach event handlers respectively.