Skip to content

Tagging

DotSelect supports tagging — allowing users to create new options on the fly by typing values that do not exist in the dropdown.

Enabling Tagging

Add data-create-items="true" to allow users to type and create new options:

When the user types a value that does not match any existing option, an "Add ..." prompt appears in the dropdown. Pressing Enter or clicking the prompt adds the new value.

How the "Add..." Prompt Works

  1. The user types into the search input
  2. DotSelect filters existing options as usual
  3. If no exact match is found and data-create-items is enabled, an "Add ..." option appears at the bottom of the dropdown
  4. Clicking the prompt (or pressing Enter) creates a new <option> element and selects it
  5. The new option's value and text are both set to the typed string

Customizing the Prompt Text

Use the locale system to customize the "Add" prompt:

The {@term} token is replaced with what the user typed.

Tags Plugin

For a more polished tagging experience, enable the tags plugin. This adds comma-separated input and paste support:

Comma Separator

With the tags plugin, users can type multiple values separated by commas. Each segment becomes a separate tag:

javascript, python, rust

This creates three separate tags: javascript, python, and rust.

Custom Separator

Change the separator character using data-tags-separator:

Paste Support

The tags plugin supports pasting comma-separated (or custom-separated) values. When a user pastes text like:

react, vue, angular, svelte

DotSelect automatically splits the pasted text by the separator and creates individual tags for each value.

TIP

Paste support works from spreadsheets too. If a user copies a column of cells from Excel or Google Sheets, each cell value becomes a separate tag.

Validation and Duplicates

DotSelect prevents duplicate tags by default. If the user tries to add a tag that already exists, it is ignored.

Restricting Allowed Values

Combine data-create-items with data-create-items-pattern to validate new tags against a regex pattern:

If the typed value does not match the pattern, the "Add" prompt does not appear.

Combining with AJAX

Tagging can be combined with AJAX search. Users can search existing server-side options and create new ones:

WARNING

When using tagging with AJAX, newly created tags exist only on the client. Your form submission handler should be prepared to handle new values that do not yet exist in the database.

Styling Tags

Tags inherit the default DotSelect item styling. Customize their appearance with CSS:

css
.dot-select .ds-item {
  background-color: #e0f2fe;
  border: 1px solid #7dd3fc;
  border-radius: 9999px;
  padding: 2px 8px;
  font-size: 0.875rem;
}

Or use the remove-button plugin alongside tags for a polished look:

Tagging Configuration

AttributeDescriptionDefault
data-create-itemsAllow creating new optionsfalse
data-create-items-textText for the "Add" prompt. Use {@term} for the typed valueAdd "{@term}"
data-create-items-patternRegex pattern to validate new values
data-plugins="tags"Enable the tags plugin for comma-separated input
data-tags-separatorSeparator character for the tags plugin,

Complete Example

A full tagging setup with remove buttons, paste support, and existing options:

MIT Licensed