--- title: Advanced select seoTitle: Advanced select (Tom Select) order: 8 summary: Tom Select turns a `` and replaces it with a searchable dropdown. The original ` ## Installation Install Tom Select with npm: Or include it from a CDN. You need both the script and its stylesheet: `} /> ### Compiling the Sass sources Use the compiled `tom-select.bootstrap5.css`. Tom Select's Sass sources read Bootstrap variables such as `$border-color` and pass them to `color.adjust()`, and in Tabler those variables hold CSS custom properties like `var(--gray-200)`, so Sass stops with `$color: var(--gray-200) is not a color`. If you still want to compile it, set the variables Tom Select adjusts to plain colors first: ```scss // `as *` keeps Tabler's variables visible to the @import below @use '@tabler/core/scss/tabler' as *; $select-color-text: #182433; $select-color-item-border: #e6e7e9; @import 'tom-select/dist/scss/tom-select.bootstrap5'; ``` ## Usage ### Basic select Add `class="form-select"` to a ` ```js new TomSelect('#select-basic', { copyClassesToDropdown: false, }); ``` ### Placeholder Native ` ### Multiple values Add the `multiple` attribute to let users pick more than one option. Tom Select shows each pick as a removable tag. ```js new TomSelect('#select-multiple', { copyClassesToDropdown: false, }); ``` ### Optgroups Group related options with ``. Tom Select shows the group label in the dropdown and keeps it searchable. ### Validation states Add `.is-valid` or `.is-invalid` to the `
### Rich options An option can carry extra markup - an avatar, a flag, a badge - through a `data-custom-properties` attribute. Read it in a custom `render.option` / `render.item` function and show it next to the option text. ```js function renderOption(data, escape) { if (data.customProperties) { return ``; } return `
${escape(data.text)}
`; } new TomSelect('#select-avatar', { copyClassesToDropdown: false, dropdownParent: 'body', render: { item: renderOption, option: renderOption, }, }); ``` Tom Select turns a `data-custom-properties` attribute into `data.customProperties` on the option object, so `render.option` and `render.item` can read it. `escape()` keeps user-supplied text safe when it is inserted as HTML. ### Common options These are the options you will need most often. Tom Select has more, and they are listed in its [documentation](https://tom-select.js.org/docs/). | Option | What it does | | --- | --- | | `copyClassesToDropdown` | Copies the `` with `for` and `id`. Tom Select's generated markup does not replace the need for a label. - The dropdown can be operated with the keyboard: type to search, arrow keys to move between options, Enter to pick one, and Backspace to remove the last tag on a multi-value select. - When using `render.option`, keep enough text content in the rendered HTML - an avatar or flag alone does not tell a screen reader user which option it is. - Escape user-supplied text in custom render functions with the `escape` argument, so option text can never break out of the generated HTML.