Skip to content

Chained Selects

DotSelect supports declarative parent-child select relationships where the options in a child select depend on the selected value of its parent. This is configured entirely through HTML attributes.

How Chaining Works

  1. A group of selects is linked by a shared data-chain-group name
  2. Each select in the chain is assigned an index with data-chained-index (starting at 0)
  3. When a parent select's value changes, all child selects reload their options based on the parent's selection
  4. Empty levels are automatically skipped

Static Chained Selects

For static data, use data-chained-child-data to provide a JSON mapping of parent values to child options:

AJAX Chained Selects

For dynamic data, use data-chained-child-ajax-data to specify the AJAX URL with a token for the parent value:

TIP

In chained AJAX URLs, {@val} refers to the parent select's current value, not the current select's value. This is what creates the parent-child relationship.

Chaining Configuration

AttributeDescriptionExample
data-chain-groupName that links selects into a chain"location"
data-chained-indexPosition in the chain (0-based)0, 1, 2
data-chained-child-dataJSON map of parent values to child options'{"us": [...]}'
data-chained-child-ajax-dataAJAX URL template for loading child options"/api/states?country={@val}"
data-chained-disabled-on-emptyDisable the select when parent has no value"true"
data-chained-clear-on-parent-changeClear value when parent changes"true"

Auto-Skip Empty Levels

If a parent value results in no options for a child, DotSelect can automatically skip that level and move to the next child that has options:

When data-chained-skip-empty="true" is set and the AJAX response returns an empty array, DotSelect hides the current select and passes the parent value directly to the next child in the chain.

Disabling Children Until Parent is Selected

By default, child selects are disabled until their parent has a value:

JavaScript Chain API

For advanced use cases, you can interact with chains programmatically:

js
// Initialize a chain group
const chain = DotSelect.chain('location');

// Get all current values in the chain
const values = chain.getValues();
// { 0: "us", 1: "ca", 2: "sf" }

// Get a specific level's value
const state = chain.getValue(1);

// Listen for changes at any level
chain.onChange((index, value, instance) => {
  console.log(`Level ${index} changed to ${value}`);
});

// Wait for the chain to be fully initialized
chain.whenReady(() => {
  console.log('All chain members are ready');
});

// Get all DotSelect instances in the chain
const members = chain.getMembers();

// Destroy the chain
chain.destroy();

Pre-Selected Chain Values

You can pre-select values across the entire chain. DotSelect resolves them in order:

WARNING

When pre-selecting values in an AJAX chain, make sure each child level has data-ajax-resolve-url configured so it can resolve the selected ID into display text.

Country, State, City Example

A complete, copy-paste ready example:

MIT Licensed