Skip to main content
Login

Sign in to access your account

Custom Select - National Design System

A form field that opens a styled list, where the user picks one option

Overview

A custom select is a field that opens a list of options under it. The user picks one option, and the field shows its label. The field has three parts: a read-only text box for the label, a hidden input for the value, and a list of option buttons. An option can also hold an icon and a second line of text.

Pick another component when:

  • the user types to search a long list: Autocomplete
  • the user picks more than one option: Multiselect
  • the design asks for the phone’s own picker: the native select in Text Fields

Markup

Options
Structure
Size
Style
State
More
Preview
<div class="nds-form-container nds-select"> <div class="nds-form-header"> <label for="custom-select-region"> <span class="nds-label">Region</span> </label> </div> <div class="nds-form-control"> <input type="text" id="custom-select-region" class="nds-input nds-select-input" placeholder="Choose a region" readonly> <input type="hidden" name="region" class="nds-select-value"> <div class="nds-select-dropdown" hidden> <div class="nds-select-options"> <button type="button" class="nds-btn nds-subtle nds-select-option" data-value="riyadh"> <span class="nds-option-text"> <span class="nds-label">Riyadh</span> </span> </button> <button type="button" class="nds-btn nds-subtle nds-select-option" data-value="makkah"> <span class="nds-option-text"> <span class="nds-label">Makkah</span> </span> </button> <button type="button" class="nds-btn nds-subtle nds-select-option" data-value="eastern"> <span class="nds-option-text"> <span class="nds-label">Eastern Province</span> </span> </button> <button type="button" class="nds-btn nds-subtle nds-select-option" data-value="asir"> <span class="nds-option-text"> <span class="nds-label">Asir</span> </span> </button> </div> </div> </div> </div>

Behavior

Option Content

An option holds its label in .nds-label inside .nds-option-text. A .nds-description beside the label adds a second line, and an icon before .nds-option-text sits on the label’s line. The field shows the label only, never the description or the icon.

Saved Value

To show a saved value, write it in the value of .nds-select-value, and leave the text box empty. When the page loads, the script finds the option with the same data-value and writes its label in the text box. When the list opens, that option shows as picked.

Disabled and Read-only

data-state~="disabled" on .nds-select disables both inputs, so the user cannot open the list and the value does not post. With data-state~="readonly", the list does not open by mouse or by keyboard. The value still posts.

Built-in Features

  • Auto-initialization

    Every .nds-select-input works with no setup. The script builds a field's list the first time the user focuses it.

  • Form Value

    The hidden .nds-select-value holds the picked option's data-value and posts with the form. The text box shows the label and does not post.

  • Keyboard Support

    Enter, Space and the arrow keys open the list on the picked option. The arrow keys move through the options, and Escape closes the list and returns focus to the field.

  • Required Check

    With data-required, the form checks the hidden value at submit. An empty field shows "Please select an option" in the page language and blocks the submit.

  • List Position

    The list opens under the field, as wide as the field. In a modal or a scrolling box, it moves to <body>, so nothing clips it.

Best Practices

  • Use a custom select for a single choice in a form, whatever the number of options. A native select beside styled fields does not match them.
  • Put the name on .nds-select-value only.
  • Write readonly on .nds-select-input. It stops the user from typing in the text box.
  • Write hidden on .nds-select-dropdown, so the list stays closed until the script builds it.
  • Give each option a unique data-value. setValue() and a saved value pick the first option that matches.
  • Put data-required on .nds-select, not on the text box. The form reads the hidden value.
  • Keep a description to one short line.
  • Write aria-hidden="true" on an option’s icon. The label names the option.
  • To change the value from a script, use setValue() or clear(). They update the label, the value and the selected state together.
  • For the label, info text, feedback and the required mark, see Text Fields. They work the same on every field.

API

Other Classes

Class Element Effect
.nds-select-menu .nds-select-dropdown The script adds it when it builds the list. Style the list with it: it stays on the list when the list moves to <body>
.nds-dropmenu, .nds-dropmenu-trigger, .nds-dropmenu-menu, .nds-dropmenu-scroll, .nds-dropmenu-item .nds-form-control, .nds-select-input, .nds-select-dropdown, .nds-select-options, each .nds-select-option, in the same order The script adds them when it builds the list, so Dropmenu opens, places and closes it. Do not write them

Data Attributes

Attribute Element Effect
data-required .nds-select The form needs a pick. It does not submit while .nds-select-value is empty
data-error-message .nds-select Replaces the message the required check shows
data-state~="disabled", data-state~="readonly" .nds-select Set it yourself. See Disabled and Read-only
data-state~="open" .nds-select The script sets it when the list opens, and removes it when the list closes. It turns the arrow
role="combobox" .nds-select-input The script sets it when it builds the list. Dropmenu then sets aria-expanded on it
hidden .nds-select-dropdown Write it. The script removes it when it builds the list
data-value .nds-select-option The value the option writes in .nds-select-value
data-state~="selected" .nds-select-option The script sets it on the picked option, and removes it from the others, at each pick, setValue() and clear(). Do not write it: write the value in .nds-select-value

CSS Custom Properties

Property Default Controls
--dropmenu-min-width 100% The least width of the list. 100% is the width of the field. Set it on .nds-select. The other Dropmenu width properties work here too

Keyboard

Key Where Effect
Enter, Space text box Opens or closes the list, and focuses the picked option or the first one
Arrow Down, Arrow Up text box Opens the list, and focuses the picked option, or the first or last one
Arrow Down, Arrow Up, Home, End list Moves through the options
Enter, Space an option Picks it, closes the list and returns focus to the text box
Escape list Closes the list and returns focus to the text box

JavaScript

el can be the .nds-select-input, an element that holds it, such as its .nds-form-control or .nds-form-container, its id or a selector.

Method Effect
NDS.CustomSelect.init() Writes the label of every saved value. The first call also starts to watch the page for focus and picks. The loader calls it on load. Call it again after you add a field with a saved value
NDS.CustomSelect.create(el) Builds the list of one field now, instead of at its first focus. A built field is left as it is. Call it, or focus the field, before a script opens it with click(): a click with no focus does not open an unbuilt field
NDS.CustomSelect.getValue(el) Returns the data-value of the picked option, or '' when none is picked or the field is not found
NDS.CustomSelect.setValue(el, 'makkah') Picks the option with that data-value, the same as a click, and returns true. It returns false, and changes nothing, when no option has the value. It works before the list is built
NDS.CustomSelect.clear(el) Empties the text box and the value, removes the selected state, and returns true. It returns false when the field is not found
formControl.ndsCustomSelect.getValue(), .setValue(value), .clear() The same, on a field that has built its list
NDS.CustomSelect.destroy(el) Undoes create() for one field: pass the input or its field. The list is hidden again and the field builds again at its next focus. Call it before you remove the field from the page
NDS.CustomSelect.validate(el, options) Checks the field and returns { valid, message, value }. Pass { showMessage: false } to check without a message
Event Fired on Detail
nds:custom-select:change .nds-form-control. It does not bubble { value, text }: the picked option’s data-value and label. Empty strings after clear(). It fires at each pick, setValue() and clear()
input, change .nds-select-input and .nds-select-value, and they bubble Native events, with no detail. They fire with nds:custom-select:change
nds:dropmenu:opened, nds:dropmenu:closed .nds-form-control, and they bubble The list opened or closed. See Dropmenu
var field = document.querySelector('#custom-select-region').closest('.nds-form-control'); // Log each pick field.addEventListener('nds:custom-select:change', function (e) { console.log(e.detail.value, e.detail.text); }); // Show a saved value NDS.CustomSelect.setValue(field, 'makkah');

The full API is in the banner of _js/nds-custom-select.js.

Last Modified Date: 01/11/2026 - 05:15 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks