---
title: API
tags: docs
---
{% from "macro_config.njk" import config_table %}
Instances of Tom Select can be controlled programmatically via the methods described in this section.
{% highlight "js" %}
var control = new TomSelect('#select');
control.addOption({value:'test'});
control.addItem('test');
{% endhighlight %}
Method |
Description |
addOption(data) |
Adds an available option, or array of options. If it already exists, nothing will happen. Note: this does not refresh the options list dropdown (use refreshOptions() for that). |
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. |
clearOptions() |
Removes all unselected options from the control. To clear selection options, call clear() before calling clearOptions() |
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. |
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, [triggerDropdown], [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. |
refreshItems() |
Re-renders the selected item lists. |
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(template) |
Clears the render cache. Takes an optional template argument (e.g. option , item ) to clear only that cache. |
updatePlaceholder() |
When the `settings.placeholder` value is changed, the new placeholder will be displayed. |
setTextboxValue(str) |
Sets the value of the input field. |