API

Instances of Tom Select can be controlled programmatically via the methods described in this section.

var control = new TomSelect('#select');
control.addOption({value:'test'});
control.addItem('test');

To address an already existing Tom Select control, you can get it with:

let select = document.getElementById('#select');
let control = select.tomselect;

Constructor

new TomSelect(input_element,settings);

Where input_element is a NodeDefinition of an Input Element or Select Element. Note: using CSS Selectors that match multiple elements will only create a Tom Select instance on the first matching element.

Option Methods

Method Description
addOption(data,user_created=false) Adds an available option. If it already exists, nothing will happen. Options created with user_created=true will not show up as available options once they are unselected if settings.persist=false Note: this does not refresh the options list dropdown (use refreshOptions() for that).
addOptions(data[],user_created=false) Adds an array of options
updateOption(value, data) Updates an option available for selection. If it is visible in the selected items or options dropdown, it will be re-rendered automatically.
removeOption(value) Removes the option identified by the given value.
getOption(value,create=false) Retrieves the dom element for the option identified by the given value. If create is true, the dom element will be created if it hasn't already been created.
getAdjacent(HTMLElement, direction) Retrieves the dom element for the previous or next option, relative to the currently highlighted option. The direction argument should be 1 for "next" or -1 for "previous".
refreshOptions(triggerDropdown) Refreshes the list of available options shown in the autocomplete dropdown menu.
clearOptions(clearFilter?)

Removes all unselected options from the control. To clear selection options, call clear() before calling clearOptions().

As of v2.1, a custom clearFilter() can be passed as an argument to control which options are removed.

clearFilter(option,value) Callback used by clearOptions() to decide whether or not an option should be removed. Return true to keep an option, false to remove

Item Methods

Method Description
clear(silent) Resets / clears all selected items from the control. If silent is truthy, no change event will be fired on the original input.
getItem(value) Returns the dom element of the item matching the given value.
addItem(value, silent) "Selects" an item. Adds it to the list at the current caret position. If silent is truthy, no change event will be fired on the original input.
removeItem(value|HTMLElement, silent) Removes the selected item matching the provided value. If silent is truthy, no change event will be fired on the original input.
createItem(value, [callback]) Invokes the create method provided in the settings that should provide the data for the new item, given the user input. Once this completes, it will be added to the item list. Prior to v2.1.1, the method's signature was createItem( value, [triggerDropdown], and [callback]).
refreshItems() Re-renders the selected item lists.

Optgroup Methods

Method Description
addOptionGroup(id, data) Registers a new optgroup for options to be bucketed into. The id argument refers to a value of the property in option identified by the optgroupField setting.
removeOptionGroup(id) Removes a single option group.
clearOptionGroups() Removes all existing option groups.

Event Methods

Method Description
on(event, handler) Adds an event listener.
off(event, handler) Removes an event listener.
off(event) Removes all event listeners.
trigger(event, ...) Triggers event listeners.
Method Description
open() Shows the autocomplete dropdown containing the available options.
close() Closes the autocomplete dropdown menu.
positionDropdown() Calculates and applies the appropriate position of the dropdown.

Other Methods

Method Description
destroy() Destroys the control and unbinds event listeners so that it can be garbage collected.
load(query) Initiates loading options from your remote data provider for the given query string. See the load() callback
focus() Brings the control into focus.
blur() Forces the control out of focus.
lock() Disables user input on the control (note: the control can still receive focus).
unlock() Re-enables user input on the control.
disable() Disables user input on the control completely. While disabled, it cannot receive focus.
enable() Enables the control so that it can respond to focus and user input.
getValue() Returns the value of the control. If multiple items can be selected with a "select" input tag (e.g. <select multiple>), this returns an array. Otherwise, returns a string (separated by delimiter if "multiple").
setValue(value, silent) Resets the selected items to the given value.
setCaret(index) Moves the caret to the specified position (index being the index in the list of selected items).
isFull() Returns whether or not the user can select more items.
clearCache() Clear cached option renderings.
setTextboxValue(str) Sets the value of the input field.
sync() Synchronizes the Tom Select instance with the underlying <select> or <input>
Option Description Type
options Original search options. object
query The raw user input. string
tokens An array containing parsed search tokens. A token is an object containing two properties: string and regex . array
total The total number of results. int
items A list of matched results. Each result is an object containing two properties: score and id . array