A vanilla JS customisable text input/select box plugin
npx @tessl/cli install tessl/npm-choices--js@10.2.0Choices.js is a lightweight, vanilla JavaScript library for creating customizable select boxes and text inputs with search, filtering, and multi-selection capabilities. This comprehensive documentation covers all aspects of the library for AI agents and developers.
npm install choices.jsnpm install choices.jsyarn add choices.jsimport Choices from 'choices.js';
import {
DEFAULT_CONFIG,
DEFAULT_CLASSNAMES,
EVENTS,
KEY_CODES,
templates
} from 'choices.js';const Choices = require('choices.js');
const {
DEFAULT_CONFIG,
DEFAULT_CLASSNAMES,
EVENTS,
KEY_CODES,
templates
} = require('choices.js');<script src="path/to/choices.js"></script>
<script>
const choices = new Choices('#my-select');
</script>// Transform a basic select element
const element = document.querySelector('#my-select');
const choices = new Choices(element, {
searchEnabled: true,
placeholder: true,
placeholderValue: 'Choose an option...'
});const multiSelect = new Choices('#multi-select', {
removeItemButton: true,
maxItemCount: 5,
searchResultLimit: 4,
renderSelectedChoices: 'always'
});const textInput = new Choices('#text-input', {
delimiter: ',',
editItems: true,
maxItemCount: -1,
removeItemButton: true
});const choices = new Choices('#dynamic-select');
// Set choices programmatically
choices.setChoices([
{ value: 'choice1', label: 'Choice 1', selected: true },
{ value: 'choice2', label: 'Choice 2', disabled: false },
{ value: 'choice3', label: 'Choice 3', customProperties: { description: 'Custom data' } }
], 'value', 'label', true);
// Listen for changes
element.addEventListener('change', (event) => {
console.log('Selection changed:', event.detail);
});Choices.js follows a component-based architecture with several key systems:
Choices Instance
├── PassedElement (original input/select)
├── ContainerOuter (main wrapper)
│ ├── ContainerInner (inner wrapper)
│ │ ├── ItemList (selected items)
│ │ └── Input (search/input field)
│ └── Dropdown (choice list container)
│ └── ChoiceList (available options)The main Choices class provides comprehensive control over select elements and text inputs. Essential methods include initialization, value management, and lifecycle control.
const choices = new Choices(element, config);
choices.init(); // Initialize component
choices.setValue(['value1']); // Set selected values
choices.getValue(); // Get current values
choices.destroy(); // Clean up instance→ See Core Usage for complete API reference
Over 45 configuration options control every aspect of behavior, appearance, and functionality.
{ .api }
interface Options {
// Behavioral options
searchEnabled: boolean;
removeItemButton: boolean;
editItems: boolean;
maxItemCount: number;
// Display options
allowHTML: boolean;
placeholder: boolean;
placeholderValue: string;
// Advanced options
fuseOptions: object;
callbackOnInit: Function;
// ... 40+ more options
}→ See Configuration for all options and defaults
Comprehensive APIs for managing choices, selected items, and values with full programmatic control.
// Choice management
choices.setChoices(choicesArray, 'value', 'label', replaceChoices);
choices.clearChoices();
// Item management
choices.setChoiceByValue('specific-value');
choices.removeActiveItemsByValue('remove-value');
choices.highlightItem(item, runEvent);→ See Data Management for complete data APIs
Rich event system with 9 event types for monitoring user interactions and state changes.
{ .api }
type EventType =
| 'addItem'
| 'removeItem'
| 'highlightItem'
| 'unhighlightItem'
| 'choice'
| 'change'
| 'search'
| 'showDropdown'
| 'hideDropdown'
| 'highlightChoice';→ See Events for event handling and details
Modular template system with 12 template functions for complete UI customization.
{ .api }
interface Templates {
containerOuter: Function;
containerInner: Function;
itemList: Function;
placeholder: Function;
item: Function;
choiceList: Function;
choiceGroup: Function;
choice: Function;
input: Function;
dropdown: Function;
notice: Function;
option: Function;
}→ See Templates for customization guide
Comprehensive TypeScript support with interfaces for all data structures and configuration.
{ .api }
interface Choice {
id: number;
label: string;
value: string;
selected: boolean;
disabled: boolean;
customProperties?: Record<string, any>;
}
interface Item extends Choice {
choiceId: number;
highlighted: boolean;
}→ See Types for complete type definitions
npm install choices.jsimport Choices from 'choices.js';const choices = new Choices('#my-select', options);change, addItem, removeItem eventssetChoices(), setValue(), getValue() methodsThis documentation provides complete coverage of Choices.js v10.2.0 for effective library usage without requiring access to source code.