Skip to content

Methods ​

DotSelect provides a rich JavaScript API for programmatic control. Methods are available on DotSelect instances, as static methods on the DotSelect class, and on chain objects.

Getting an Instance ​

js
// Create a new instance
const select = new DotSelect(document.querySelector('#my-select'));

// Or get an existing instance
const select = DotSelect.getInstance(document.querySelector('#my-select'));

Instance Methods ​

setValue(value) ​

Set the current value. Accepts a string (single) or an array of strings (multiple).

js
// Single select
select.setValue('us');

// Multiple select
select.setValue(['js', 'py', 'go']);

Parameters:

NameTypeDescription
valuestring | string[]The value(s) to select

Returns: DotSelect — the instance (chainable)


getValue() ​

Get the current value.

js
// Single select
const value = select.getValue(); // "us"

// Multiple select
const values = select.getValue(); // ["js", "py", "go"]

Returns: string | string[]


getData() ​

Get the full data objects for the currently selected option(s).

js
const data = select.getData();
// Single: { id: "us", text: "United States" }
// Multiple: [{ id: "js", text: "JavaScript" }, { id: "py", text: "Python" }]

Returns: object | object[]


clear() ​

Clear the current selection.

js
select.clear();

Returns: DotSelect


enable() ​

Enable the select (remove disabled state).

js
select.enable();

Returns: DotSelect


disable() ​

Disable the select.

js
select.disable();

Returns: DotSelect


open() ​

Open the dropdown programmatically.

js
select.open();

Returns: DotSelect


close() ​

Close the dropdown programmatically.

js
select.close();

Returns: DotSelect


addOption(data, selected) ​

Add a new option to the select.

js
select.addOption({ id: 'new', text: 'New Option' });

// Add and select it immediately
select.addOption({ id: 'new', text: 'New Option' }, true);

Parameters:

NameTypeDescription
dataobjectOption data with at least id and text
selectedbooleanWhether to select the new option (default: false)

Returns: DotSelect


removeOption(value) ​

Remove an option by its value.

js
select.removeOption('old-value');

Parameters:

NameTypeDescription
valuestringThe value of the option to remove

Returns: DotSelect


clearOptions() ​

Remove all options from the select.

js
select.clearOptions();

Returns: DotSelect


reload() ​

Reload options from the AJAX source. Useful when external data has changed.

js
select.reload();

Returns: DotSelect


refresh() ​

Re-render the component without reloading data. Useful after DOM changes.

js
select.refresh();

Returns: DotSelect


updateSettings(settings) ​

Update configuration settings at runtime.

js
select.updateSettings({
  placeholder: 'New placeholder...',
  searchable: true,
  maxItems: 5,
});

Parameters:

NameTypeDescription
settingsobjectKey-value pairs of settings to update

Returns: DotSelect

WARNING

Not all settings can be changed at runtime. Structural settings like ajax-url require calling reload() after updating.


hide() ​

Hide the entire DotSelect component.

js
select.hide();

Returns: DotSelect


show() ​

Show a previously hidden DotSelect component.

js
select.show();

Returns: DotSelect


destroy() ​

Remove DotSelect enhancements and restore the original <select> element.

js
select.destroy();

getElement() ​

Get the original <select> DOM element.

js
const el = select.getElement();
// <select data-dot-select ...>

Returns: HTMLSelectElement


getWrapper() ​

Get the DotSelect wrapper DOM element.

js
const wrapper = select.getWrapper();
// <div class="dot-select ...">

Returns: HTMLElement


getSettings() ​

Get the current resolved settings object.

js
const settings = select.getSettings();
console.log(settings.placeholder); // "Choose..."
console.log(settings.searchable);  // true

Returns: object


getOptionCount() ​

Get the total number of options currently in the select.

js
const count = select.getOptionCount();

Returns: number


isOpen() ​

Check whether the dropdown is currently open.

js
if (select.isOpen()) {
  select.close();
}

Returns: boolean


isHidden() ​

Check whether the component is currently hidden.

js
if (select.isHidden()) {
  select.show();
}

Returns: boolean


Static Methods ​

DotSelect.init(root?) ​

Initialize all <select data-dot-select> elements. Optionally limit to a subtree.

js
// Initialize all
DotSelect.init();

// Initialize within a container
DotSelect.init(document.querySelector('#form-container'));

Parameters:

NameTypeDescription
rootHTMLElementOptional root element to search within (default: document)

DotSelect.chain(groupName) ​

Get or create a chain controller for the named group.

js
const chain = DotSelect.chain('location');

Parameters:

NameTypeDescription
groupNamestringThe chain group name (matches data-chain-group)

Returns: ChainController


DotSelect.getInstance(element) ​

Get the DotSelect instance associated with a DOM element, or null if not initialized.

js
const instance = DotSelect.getInstance(document.querySelector('#my-select'));

Parameters:

NameTypeDescription
elementHTMLSelectElementThe original select element

Returns: DotSelect | null


DotSelect.destroyAll() ​

Destroy all DotSelect instances on the page, restoring original select elements.

js
DotSelect.destroyAll();

DotSelect.plugin(name, factory) ​

Register a custom plugin.

js
DotSelect.plugin('my-plugin', (instance, options) => {
  return {
    init() { /* called on initialization */ },
    destroy() { /* called on destroy */ },
    onOpen() { /* called when dropdown opens */ },
  };
});

Parameters:

NameTypeDescription
namestringPlugin identifier (used in data-plugins)
factoryfunctionFactory function receiving (instance, options)

Chain API ​

The chain controller returned by DotSelect.chain() provides these methods:

chain.getValues() ​

Get all current values across the chain as an object keyed by index.

js
const chain = DotSelect.chain('location');
const values = chain.getValues();
// { 0: "us", 1: "ca", 2: "sf" }

Returns: object


chain.getValue(index) ​

Get the value at a specific index in the chain.

js
const state = chain.getValue(1); // "ca"

Parameters:

NameTypeDescription
indexnumberThe chain index

Returns: string | null


chain.onChange(callback) ​

Register a callback that fires when any member of the chain changes.

js
chain.onChange((index, value, instance) => {
  console.log(`Level ${index} changed to "${value}"`);
});

Parameters:

NameTypeDescription
callbackfunctionReceives (index: number, value: string, instance: DotSelect)

Returns: ChainController


chain.whenReady(callback) ​

Register a callback that fires once all chain members are initialized.

js
chain.whenReady(() => {
  console.log('Chain is fully ready');
});

Parameters:

NameTypeDescription
callbackfunctionCalled when all members are initialized

Returns: ChainController


chain.getMembers() ​

Get all DotSelect instances in the chain, ordered by index.

js
const members = chain.getMembers();
// [DotSelect (index 0), DotSelect (index 1), DotSelect (index 2)]

Returns: DotSelect[]


chain.destroy() ​

Destroy the chain controller and unlink all members (does not destroy individual instances).

js
chain.destroy();

MIT Licensed