Overview
A multiselect is a form field with a Dropmenu at its start. The menu holds a group of checkboxes, and each checked option shows in the field as a chip. The checkboxes are the values: the form submits the checked ones, with no hidden inputs.
Pick another component when:
Markup
Variants
A row on .nds-checkbox changes every option row. A Reset or Apply row inserts its part at the end of .nds-dropmenu-menu, after .nds-dropmenu-scroll. Search changes two elements: write both.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Manual (default) | — | — | You write one checkbox per option in the menu, in fieldsets with a legend. Use it when the server writes the options |
| Structure | Data-driven (hint: The script builds the options from JSON) | canon #multiselect-json |
— | An empty menu with the options as JSON in data-multiselect-options. The script builds the checkboxes. Use it for options that come from data |
| Menu button (any) | Icon only (limit: 1 button content) | [aria-label="Select"] |
.nds-dropmenu-trigger:has(.nds-icon) |
A narrow button, for a narrow field. The aria-label names the button, and the script adds the chosen options to it. Not with Label only |
| Menu button (any) | Icon only (limit: 1 button content) | remove | .nds-dropmenu-trigger:has(.nds-icon) > .nds-label |
Removes the visible word |
| Menu button (any) | Label only (limit: 1 button content) | [type="button"] |
.nds-dropmenu-trigger:has(.nds-label) |
The word with no icon. Not with Icon only |
| Menu button (any) | Label only (limit: 1 button content) | remove | .nds-dropmenu-trigger:has(.nds-label) > .nds-icon |
Removes the icon |
| Menu button (any) | Solid | canon #multiselect-trigger-solid |
[data-multiselect-dropmenu] (start) |
A filled button that stands out from the field |
| Menu button (any) | Solid | remove | .nds-dropmenu-trigger.nds-subtle |
Removes the subtle button |
| Menu button (any) | MD (hint: A smaller 32px menu button) | .nds-md |
.nds-dropmenu-trigger |
A 32px button with a smaller word and icon |
| Menu options | None (default) | — | — | No buttons and no search. Each check commits at once |
| Menu options | Reset (hint: Unchecks every option) | canon #multiselect-footer-reset |
.nds-dropmenu-menu |
A button that unchecks every option. See Reset Button |
| Menu options | Apply (hint: Checks wait until Apply) | canon #multiselect-footer-apply |
.nds-dropmenu-menu |
Checks wait in the menu until the user presses Apply. See Apply Button |
| Menu options | Search (hint: Filters the options) | [data-search] |
[data-multiselect-dropmenu] |
A search box at the top of the menu hides the options that do not match. Use it for a long list |
| Menu options | Search (hint: Filters the options) | [data-search-item] |
.nds-checkbox |
Put it on each option row. Data-driven adds it for you |
| Menu options | Reset + Apply | canon #multiselect-footer-both |
.nds-dropmenu-menu |
Both buttons in one row. Reset unchecks the options in the menu, and Apply commits them |
| Menu options | Reset + Search | canon #multiselect-footer-reset |
.nds-dropmenu-menu |
Reset and the search box |
| Menu options | Reset + Search | [data-search] |
[data-multiselect-dropmenu] |
The same |
| Menu options | Reset + Search | [data-search-item] |
.nds-checkbox |
The same |
| Menu options | Apply + Search | canon #multiselect-footer-apply |
.nds-dropmenu-menu |
Apply and the search box |
| Menu options | Apply + Search | [data-search] |
[data-multiselect-dropmenu] |
The same |
| Menu options | Apply + Search | [data-search-item] |
.nds-checkbox |
The same |
| Menu options | Reset + Apply + Search | canon #multiselect-footer-both |
.nds-dropmenu-menu |
Both buttons and the search box |
| Menu options | Reset + Apply + Search | [data-search] |
[data-multiselect-dropmenu] |
The same |
| Menu options | Reset + Apply + Search | [data-search-item] |
.nds-checkbox |
The same |
| Chip color | Primary (default) | — | — | Chips in the primary color |
| Chip color | Neutral | [data-chip-class="nds-neutral nds-sm"] |
.nds-multiselect |
Chips in the neutral color, for a choice that is not a brand action |
| State | None (default) | — | — | The user can change the options |
| State | Disabled | [data-state~="disabled"] |
.nds-multiselect |
The user cannot open the menu or remove a chip |
| State | Read-only | [data-state~="readonly"] |
.nds-multiselect |
The menu opens and shows the options, but the user cannot change them or remove a chip |
| Validation (any) | Required (hint: Press Validate with nothing checked) | [data-required] |
.nds-multiselect |
At least one option must be checked before the form submits. Say the rule in the label |
| Validation (any) | At least 2 (hint: Press Validate with one option checked) | [data-min-checked="2"] |
.nds-multiselect |
At least 2 options must be checked. Say the rule in the label |
| Validation (any) | At most 3 (hint: Check four options, then press Validate) | [data-max-checked="3"] |
.nds-multiselect |
No more than 3 options may be checked. Say the rule in the label |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
Behavior
Data-driven Options
The script builds the option rows from the JSON in data-multiselect-options on .nds-multiselect. It puts them in the empty .nds-dropmenu-scroll. The JSON is an array of values, where each value is also its label, or an object of value: label pairs. An object of objects, {"Technology": {"ai": "AI & ML"}}, makes one fieldset per key, with the key as its legend. data-multiselect-selected holds an array of the values to check at load. The built rows replace any rows in the menu. renderOptions() does the same from code.
Apply Button
With no Apply button, each check commits at once: the chips change and nds:multiselect:change fires. A button with data-multiselect-action="apply" in the menu makes checks wait. The user checks options in the menu, and Apply commits them. Closing the menu without Apply undoes them. Removing a chip undoes them too. A form submit while the menu is open checks and sends the committed options, not the waiting ones.
Reset Button
A button with data-multiselect-action="reset" in the menu unchecks every option. With no Apply button, the reset commits at once. With an Apply button, it unchecks the options in the menu only, and Apply commits the empty set. data-no-auto-close keeps the menu open after the click.
Search
data-search on the [data-multiselect-dropmenu] element adds a search box at the top of the menu. The box hides each row with data-search-item whose text does not match. data-search="20" adds the box only when the menu holds 20 rows or more, counted when the menu opens. For Data-driven, keep the empty .nds-dropmenu-scroll in the menu.
Validation
data-required, data-min-checked and data-max-checked on .nds-multiselect count the checked options when the form submits. data-required alone means at least one. The error shows on the field. After an error shows, the field checks again on each change and clears the error when the count is right.
Disabled and Read-only
data-state~="disabled" on .nds-multiselect disables the menu button and every chip. With data-state~="readonly", the menu button still works, so the user can open the menu and see all options. The options, the chips and Reset do not change anything. Code can still change the value with setValues(), removeValue(), clear() and renderOptions().
Built-in Features
-
Auto-initialization
Every
.nds-multiselecton the page starts on load, with its dropmenu. The chips for checked options show before the page appears. -
Removable Chips
A click on a chip unchecks its option and commits at once, with or without an Apply button. Focus moves to the next chip, or to the menu button after the last one.
-
Native Form Submission
The form submits each checked checkbox by its
nameandvalue. A checkbox with no name getsname="{data-multiselect-name}[]", so the server reads an array. -
Saved Values
To show a saved value, write
checkedon its options. The field builds their chips when it starts, with no script on the page. -
Whole-Field Click
A click anywhere on the field, except on a chip, opens the menu. The menu lines up with the start edge of the field.
-
Keyboard Navigation
In the open menu, the arrow keys move through the options and the menu buttons, and Space checks an option. Escape closes the menu and moves focus back to the menu button.
-
Screen Reader Support
The menu button's name lists the chosen options, such as "Select: AI & ML, Cloud". A screen reader reads each change, such as "Added Cloud", in English or Arabic.
-
Menu Placement
In a drawer, a modal or another scroll area that would cut it off, the menu moves to
<body>while it is open.
Best Practices
- Group a long list in fieldsets with a legend, and put an
<hr class="nds-divider">between the fieldsets. A screen reader reads the legend with each option. - Add the Apply button when the user checks many options before the result matters, such as a filter. Leave it off a short list.
- Give each checkbox a
data-label. The chip shows it, and the label can then hold more text than the chip needs. - Give an icon-only menu button an
aria-label, such as “Select”. The script adds the chosen options to it. - Keep the placeholder to two to four words. It shows only while no option is checked.
- Add
data-searchfor a list longer than about 15 options. - Say a count rule in the label, such as “Interests (2 to 4)”, not in the placeholder.
- For the label, info text, feedback and the required mark, see Forms. They work the same on every field.
- The field colors come from the form tokens: see Forms, Tokens.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
.nds-multiselect-menu |
.nds-dropmenu-menu |
The script adds it when the field starts. Style the menu through this class: it stays on the menu when the menu moves to <body> |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-multiselect-name |
.nds-multiselect |
The field name. The script gives it, with [] after it, to each checkbox that has no name. It is also name in the change event |
data-multiselect-options |
.nds-multiselect |
JSON options that the script builds into the menu. See Data-driven Options |
data-multiselect-selected |
.nds-multiselect |
A JSON array of the values to check at load, spelled as in the options. Used only with data-multiselect-options |
data-chip-class |
.nds-multiselect |
The classes on each chip. The default is nds-primary nds-sm. See Chips |
data-required, data-min-checked, data-max-checked |
.nds-multiselect |
The count rules. See Validation |
data-state~="filled" |
.nds-multiselect |
The script sets it when an option is checked, and removes it when none is. It hides the placeholder. destroy() removes it |
data-state~="focus" |
.nds-multiselect |
The script sets it while the menu button has focus or the menu is open, and removes it when both end |
data-state~="disabled", data-state~="readonly" |
.nds-multiselect |
Set it yourself. See Disabled and Read-only |
data-multiselect-dropmenu |
the .nds-dropmenu element |
Marks the dropmenu that holds the options. Without it, the script uses .nds-form-action.nds-prefix.nds-dropmenu |
data-anchor |
the .nds-dropmenu element |
Where the menu lines up. The script sets start when the element has no data-anchor. Write center or end to change it |
data-search |
the .nds-dropmenu element |
Adds the search box. See Search |
data-multiselect-chips |
.nds-multiselect-chips |
Marks the element that holds the chips. The script fills it. Without it, the script uses .nds-multiselect-chips |
data-no-auto-close |
each <fieldset>, and the Reset button |
A click on it leaves the menu open |
data-multiselect-action |
a button in .nds-dropmenu-footer |
reset makes a Reset button, and apply an Apply button. See Reset Button and Apply Button |
data-label |
each checkbox | The chip text. Without it, the chip shows the option’s .nds-label text |
data-search-item |
each .nds-checkbox |
The search box filters this row. Data-driven adds it when the dropmenu has data-search |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_multiselect.scss.
| Token | Preview | Value |
|---|---|---|
--form-field-text-placeholderRead by color | var(--text-secondary-paragraph)Dark mode var(--colors-neutral-400) |
JavaScript
| Method | Effect |
|---|---|
NDS.Multiselect.init() |
Starts every .nds-multiselect that has not started. The loader calls it on load. Call it again after you add a field to the page |
NDS.Multiselect.create(el) |
Starts one field, and returns its instance. On a field that has started, it returns the same instance. It returns null when the field lacks a required part |
NDS.Multiselect.destroy(el) |
Removes the listeners, the chips and the dropmenu it started, and closes an open menu. The checked options stay. A dropmenu that the page started before create() stays. Call it before you remove the field from the page |
NDS.Multiselect.validate(el, options) |
Checks the field and returns { valid, message, checked, min, max }. Pass { showMessage: false } to check without a message |
el.ndsMultiselect |
The instance of a field that has started |
NDS.Multiselect.getValues(el) |
Returns the values of the checked options, in menu order, or [] when the field is not found. el is the .nds-multiselect, its id or a selector |
NDS.Multiselect.setValues(el, values) |
Checks exactly the options in the array values, commits, and returns true. It returns false, and changes nothing, when a value has no option |
NDS.Multiselect.clear(el) |
Unchecks every option and commits. Returns true, or false when the field is not found |
instance.getValues() |
Returns the values of the checked options, in menu order |
instance.setValues(values) |
The same as NDS.Multiselect.setValues, on a field that has started. The change event fires once |
instance.apply() |
Applies the checked options: the chips change and the change event fires. The Apply button calls it |
instance.clear() |
Unchecks every option and commits. With an Apply button and the menu open, it unchecks them in the menu only. Returns true |
instance.removeValue(value) |
Unchecks one option and commits |
instance.renderOptions(options, selected) |
Replaces the options. It takes the shapes that data-multiselect-options takes, and an array of values to check. It fires no change event |
| Event | Fired on | Detail |
|---|---|---|
nds:multiselect:change |
.nds-multiselect, and it bubbles |
{ name, values, labels }, after each commit. name is data-multiselect-name. values and labels are the checked options, in the same order |
The full API is in the banner of _js/nds-multiselect.js.