Selects
<select class="select" aria-label="Fruit">
<option value="">Select a fruit</option>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="blueberry">Blueberry</option>
<option value="pineapple">Pineapple</option>
</select>
Sizes: sm, default and lg
<div class="flex flex-wrap items-center gap-3">
<select class="select" data-size="sm" aria-label="Small select">
<option>Small</option>
<option>Apple</option>
</select>
<select class="select" aria-label="Default select">
<option>Default</option>
<option>Apple</option>
</select>
<select class="select" data-size="lg" aria-label="Large select">
<option>Large</option>
<option>Apple</option>
</select>
</div>
Select with a styled list
<div id="select-theme" class="select" data-placeholder="Theme">
<button type="button" class="w-[180px]" id="select-theme-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-theme-listbox">
<span class="truncate">Theme</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div id="select-theme-popover" data-popover aria-hidden="true">
<div role="listbox" id="select-theme-listbox" aria-orientation="vertical" aria-labelledby="select-theme-trigger">
<div role="option" data-value="light">Light</div>
<div role="option" data-value="dark">Dark</div>
<div role="option" data-value="system">System</div>
</div>
</div>
<input type="hidden" name="theme" value="" />
</div>
Sizes of a select with a styled list
<div class="flex flex-wrap items-center gap-3">
<div id="select-size-sm" class="select" data-size="sm" data-placeholder="Small">
<button type="button" class="w-[180px]" id="select-size-sm-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-size-sm-listbox">
<span class="truncate">Small</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div data-popover aria-hidden="true">
<div role="listbox" id="select-size-sm-listbox" aria-orientation="vertical" aria-labelledby="select-size-sm-trigger">
<div role="option" data-value="light">Light</div>
<div role="option" data-value="dark">Dark</div>
</div>
</div>
<input type="hidden" name="size-sm" value="" />
</div>
<div id="select-size-default" class="select" data-placeholder="Default">
<button type="button" class="w-[180px]" id="select-size-default-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-size-default-listbox">
<span class="truncate">Default</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div data-popover aria-hidden="true">
<div role="listbox" id="select-size-default-listbox" aria-orientation="vertical" aria-labelledby="select-size-default-trigger">
<div role="option" data-value="light">Light</div>
<div role="option" data-value="dark">Dark</div>
</div>
</div>
<input type="hidden" name="size-default" value="" />
</div>
<div id="select-size-lg" class="select" data-size="lg" data-placeholder="Large">
<button type="button" class="w-[180px]" id="select-size-lg-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-size-lg-listbox">
<span class="truncate">Large</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div data-popover aria-hidden="true">
<div role="listbox" id="select-size-lg-listbox" aria-orientation="vertical" aria-labelledby="select-size-lg-trigger">
<div role="option" data-value="light">Light</div>
<div role="option" data-value="dark">Dark</div>
</div>
</div>
<input type="hidden" name="size-lg" value="" />
</div>
</div>
Select with a styled list and group headings
<div id="select-demo" class="select" data-placeholder="Select a fruit">
<button type="button" class="w-[180px]" id="select-demo-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-demo-listbox">
<span class="truncate">Select a fruit</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div id="select-demo-popover" data-popover aria-hidden="true">
<div role="listbox" id="select-demo-listbox" aria-orientation="vertical" aria-labelledby="select-demo-trigger">
<div role="group" aria-labelledby="select-demo-fruits">
<span role="heading" id="select-demo-fruits">Fruits</span>
<div role="option" data-value="apple">Apple</div>
<div role="option" data-value="banana">Banana</div>
<div role="option" data-value="blueberry">Blueberry</div>
<div role="option" data-value="grapes">Grapes</div>
<div role="option" data-value="pineapple">Pineapple</div>
</div>
</div>
</div>
<input type="hidden" name="select-demo-value" value="" />
</div>
Options with a title and a line of text under it
<div role="group" class="field">
<label for="select-3ds-trigger">3DS Mode</label>
<div id="select-3ds" class="select" data-placeholder="Select a mode">
<button type="button" class="w-full" id="select-3ds-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-3ds-listbox">
<span>Select a mode</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div data-popover aria-hidden="true">
<div role="listbox" id="select-3ds-listbox" aria-orientation="vertical" aria-labelledby="select-3ds-trigger">
<div role="option" data-value="off" data-label="Off">
<section>
<h3>Off</h3>
<p>Transactions are processed without 3DS authentication.</p>
</section>
</div>
<div role="option" data-value="smart" data-label="Smart" aria-selected="true">
<section>
<h3>Smart <span class="badge" data-variant="info">Recommended</span></h3>
<p>Skips 3DS in markets where shoppers rarely use it and adds 3DS on higher risk orders.</p>
</section>
</div>
<div role="option" data-value="always" data-label="Always On">
<section>
<h3>Always On</h3>
<p>Every transaction is attempted with 3DS authentication.</p>
</section>
</div>
</div>
</div>
<input type="hidden" name="three_ds_mode" value="" />
</div>
</div>
Options with an avatar, a title and a line of text; aria-disabled dims an option that cannot be chosen
<div role="group" class="field">
<label for="select-service-trigger">Tax Service</label>
<div id="select-service" class="select" data-placeholder="Select a tax service">
<button type="button" class="w-full" id="select-service-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-service-listbox">
<span>Select a tax service</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div data-popover aria-hidden="true">
<div role="listbox" id="select-service-listbox" aria-orientation="vertical" aria-labelledby="select-service-trigger">
<div role="option" data-value="next-tax" data-label="Next Tax" aria-selected="true">
<span class="avatar"><span aria-hidden="true">NT</span></span>
<section>
<h3>Next Tax</h3>
<p>Active</p>
</section>
</div>
<div role="option" data-value="avalara" data-label="Avalara" aria-disabled="true">
<span class="avatar"><span aria-hidden="true">AV</span></span>
<section>
<h3>Avalara</h3>
<p>Unavailable</p>
</section>
</div>
<div role="option" data-value="taxjar" data-label="TaxJar" aria-disabled="true">
<span class="avatar"><span aria-hidden="true">TJ</span></span>
<section>
<h3>TaxJar</h3>
<p>Unavailable</p>
</section>
</div>
</div>
</div>
<input type="hidden" name="tax_service" value="" />
</div>
</div>
Select with a search field in its list
<div id="select-searchable" class="select" data-placeholder="Select a framework">
<button type="button" class="w-[240px]" id="select-searchable-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-searchable-listbox">
<span class="truncate">Select a framework</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div id="select-searchable-popover" data-popover aria-hidden="true">
<div class="input-group">
<input type="text" role="combobox" placeholder="Search frameworks" aria-label="Search frameworks" autocomplete="off" autocorrect="off" spellcheck="false" aria-autocomplete="list" aria-expanded="false" aria-controls="select-searchable-listbox" />
<span data-align="start" aria-hidden="true"><i class="icon icon-search" aria-hidden="true"></i></span>
</div>
<div role="listbox" id="select-searchable-listbox" aria-orientation="vertical" aria-labelledby="select-searchable-trigger" data-empty="No frameworks found.">
<div role="option" data-value="next">Next.js</div>
<div role="option" data-value="sveltekit">SvelteKit</div>
<div role="option" data-value="nuxt">Nuxt.js</div>
<div role="option" data-value="remix">Remix</div>
<div role="option" data-value="astro">Astro</div>
</div>
</div>
<input type="hidden" name="framework" value="" />
</div>
A script that writes the hidden input and sends change chooses the option with that value, as on a native select
Select with more than one choice: type to filter the list
<div id="select-multiple" class="select w-[240px]" data-placeholder="Select categories">
<div class="select-chips">
<input type="text" role="combobox" aria-label="Categories" autocomplete="off" autocorrect="off" spellcheck="false" aria-autocomplete="list" aria-expanded="false" aria-controls="select-multiple-listbox" />
</div>
<div id="select-multiple-popover" data-popover aria-hidden="true">
<div role="listbox" id="select-multiple-listbox" aria-orientation="vertical" aria-multiselectable="true" data-empty="No categories found.">
<div role="option" data-value="bedding">Bedding</div>
<div role="option" data-value="furniture">Furniture</div>
<div role="option" data-value="lighting">Lighting</div>
<div role="option" data-value="stationery">Stationery</div>
<div role="option" data-value="watches">Watches</div>
</div>
</div>
<input type="hidden" name="category" value="" />
</div>
What a select with several choices posts
The form posts one field per chosen value, all with the hidden input's name, as a native multiple select does. With nothing chosen it posts no field.
category=bedding&category=lighting
One choice typed in the control, with an action above the list
<div id="select-typed" class="select w-[280px]" data-placeholder="Search or create a customer">
<div class="select-chips">
<input type="text" role="combobox" aria-label="Customer" autocomplete="off" autocorrect="off" spellcheck="false" aria-autocomplete="list" aria-expanded="false" aria-controls="select-typed-listbox" />
</div>
<div id="select-typed-popover" data-popover aria-hidden="true">
<header>
<button type="button" class="btn w-full" data-variant="outline" data-size="sm" onclick="document.getElementById('select-typed').close()">Create New Customer</button>
</header>
<div role="listbox" id="select-typed-listbox" aria-orientation="vertical" data-empty="No customers found.">
<div role="option" data-value="maya" data-label="Maya Okafor">
<section>
<h3>Maya Okafor</h3>
<p>maya.okafor@example.com</p>
</section>
</div>
<div role="option" data-value="daniel" data-label="Daniel Brandt">
<section>
<h3>Daniel Brandt</h3>
<p>daniel.brandt@example.com</p>
</section>
</div>
</div>
</div>
<input type="hidden" name="customer" value="" />
</div>
Remote options: data-url loads the options from the server
data-url on the root is the lookup address. The select needs a search field.
Every lookup address takes this request and gives this reply. q is left out while the field is empty.
GET /docs/lookup/countries/?q=fra&page=1
{
"results": [
{ "id": "fr", "text": "France" }
],
"pagination": { "more": false }
}
This site has no server, so on this page a script answers /docs/lookup/countries/.
Remote options with several choices: France is the starting choice, written into the page
<div id="select-remote-multiple" class="select w-[320px]" data-url="/docs/lookup/countries/" data-placeholder="Select countries">
<div class="select-chips">
<input type="text" role="combobox" aria-label="Countries" autocomplete="off" autocorrect="off" spellcheck="false" aria-autocomplete="list" aria-expanded="false" aria-controls="select-remote-multiple-listbox" />
</div>
<div id="select-remote-multiple-popover" data-popover aria-hidden="true">
<div role="listbox" id="select-remote-multiple-listbox" aria-orientation="vertical" aria-multiselectable="true">
<div role="option" data-value="fr" aria-selected="true">France</div>
</div>
</div>
<input type="hidden" name="countries" value="" />
</div>
Remote options with one choice: scroll the list to its end and the next page loads
<div id="select-remote" class="select" data-url="/docs/lookup/countries/" data-placeholder="Select a country">
<button type="button" class="w-[320px]" id="select-remote-trigger" aria-haspopup="listbox" aria-expanded="false" aria-controls="select-remote-listbox">
<span class="truncate">Select a country</span>
<i class="icon icon-chevron-down text-muted-foreground opacity-50 shrink-0" aria-hidden="true"></i>
</button>
<div id="select-remote-popover" data-popover aria-hidden="true">
<div class="input-group">
<input type="text" role="combobox" placeholder="Search countries" aria-label="Search countries" autocomplete="off" autocorrect="off" spellcheck="false" aria-autocomplete="list" aria-expanded="false" aria-controls="select-remote-listbox" />
<span data-align="start" aria-hidden="true"><i class="icon icon-search" aria-hidden="true"></i></span>
</div>
<div role="listbox" id="select-remote-listbox" aria-orientation="vertical" aria-labelledby="select-remote-trigger"></div>
</div>
<input type="hidden" name="country" value="" />
</div>
What a select with remote options does, case by case
| When | What happens |
|---|---|
| The page loads | No request. The options written in the markup are the starting choice. |
| The list opens | One request for page 1, with no q. The list says Searching... until the reply arrives. |
| A key is typed | The request on its way is cancelled. A new one is sent with q and page=1. |
| An older reply arrives after a newer one | It is ignored. Its request was cancelled when the newer one was sent. |
The reply has "more": true |
The listbox gets data-more and a line under the options says Loading more results... |
| The list is scrolled to within 50px of its end | One request for the next page, with the same q. Its options are added under the ones already there. Nothing is asked while a request is on its way. |
| The reply has no results | The list says No results found, or the text in data-empty on the listbox. |
| The request fails, or the reply is not JSON | The list is emptied, the listbox gets data-error and says The results could not be loaded. The error is written to the console. The next keystroke tries again. |
| An option is chosen | As in any select: the hidden input, the control and the change event. With several choices the search field is cleared, which asks for the first page again. |
| A chosen option is not in the reply | It stays in the listbox, hidden with aria-hidden, because the chips and the value are read from the chosen options. It is still chosen and still sent. |
| A chosen option is in the reply | It is shown once, in the server's order, with its tick. |
| The list closes | The request on its way is cancelled and the search field is cleared. Opening again asks again; nothing is cached. |
Tags: data-tags lets the user create an option by typing it
data-tags on the root offers the typed text as a new option. Its value is the text. The select needs a search field.
Tags: type a word that is not in the list and press Enter
Tags with remote options: choose a country from the server or create a new name
What a select with tags does, case by case
| When | What happens |
|---|---|
| Text is typed that names no option | The text is offered as the first option and is the active one, so Enter creates it. |
| Text is typed that names an option, in any letter case | No tag is offered. "RED" finds "Red". |
| The text is only spaces | No tag is offered. Spaces around the text are dropped. |
| The offered tag is chosen | It becomes an ordinary chosen option that carries data-tag. Its value and its label are the typed text. |
| The text changes before the tag is chosen | The offered tag is removed and the new text is offered. |
| A created tag is removed again | It stays in the list until the next keystroke, then it is gone. |
The select also has data-url |
The tag is offered above the first page of the server's options, unless one of them has that name. |
API Reference
Attributes
| Attribute | On | Values | Default | Description |
|---|---|---|---|---|
| data-placeholder | div.select | text | empty | The text shown while nothing is chosen. |
| data-size | div.select | "sm", "lg" | none | The height of the control. |
| aria-multiselectable | the [role=listbox] | "true" | none | Several options can be chosen. The control is then .select-chips: a chip for each choice and a text field that filters the list. Without it, a .select-chips control is one choice typed in the field. |
| data-url | div.select | an address | none | The lookup address the options are loaded from. Needs a search field. |
| data-tags | div.select | present or absent | absent | Typed text that names no option is offered as the first option. Choosing it creates it, with the text as its value. Needs a search field. |
| data-empty | the [role=listbox] | text | "No results found" | The text shown when the search matches nothing. |
| data-value | an option | text | the option's text | The value the option sends. An option with an empty value means no choice and brings the placeholder back. |
| data-label | an option | text | the option's text | The option's plain name, for one whose content is more than text. The search matches it and a chip shows it. |
| aria-selected | an option | "true" | none | A chosen option. Write it to set the starting choice. |
| aria-disabled | an option | "true" | none | The option cannot be chosen. |
| name | the hidden input | text | none | Sends the value with the form. One choice: the option's value. Several: one field per chosen value, all with this name, and no field with nothing chosen. |
| data-has-value | div.select | present or absent | set by the script | Present while something is chosen. |
| aria-busy | the [role=listbox] | "true" | set by the script | With data-url: a request is on its way. While the list is empty it says Searching... |
| data-more | the [role=listbox] | present or absent | set by the script | With data-url: the server has a further page. A line under the options says Loading more results... |
| data-error | the [role=listbox] | present or absent | set by the script | With data-url: the last request failed. The list says The results could not be loaded. |
| data-tag | an option | present or absent | set by the script | With data-tags: an option made from typed text. |
Methods and properties
| Member | Type | Description |
|---|---|---|
| value | property | The chosen value, or a list of them for several. Set it to choose an option that is in the listbox. |
| selected | property, read only | The chosen option as { value, label }, or null for none. A list of them for several. |
| open() | method | Opens the list. |
| close() | method | Closes the list. |
| refresh() | method | Draws the control again from the options, after the page has added, removed or changed them. |
Events
| Event | On | Detail | Description |
|---|---|---|---|
| change | div.select, and it bubbles | { value, selected } | Sent when the choice changes, by a click, by the keyboard or by setting value. Not sent when the same value is chosen again, or for a value a page wrote into the hidden input. |
| change | the hidden input, sent by the page | none | One choice: chooses the option with the value the page wrote into the hidden input, or no option when none has it. A script that knows only the field's name sets it as it would a native select. |
Keyboard
| Key | Description |
|---|---|
| Arrow Down, Arrow Up | On the closed control: opens the list. In the list: move between the options. |
| Home, End | Move to the first or the last option. In a search field they move the caret instead. |
| Enter | Chooses the active option. |
| Backspace | With several choices and an empty search field: removes the last chip. |
| Escape | Closes the list and returns focus to the control. |
A native select with the class select needs no script and has none of this; it is a select element.
With data-url a request is cancelled when a newer one replaces it or when the list closes.
With data-url the options are not cached: every opening asks for page 1 again.
A select with data-url or data-tags and no search field throws an error when it starts.
Once the script has started it, the element carries data-select-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.