A menu of options for single or multiple selection.
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
lightning-select enables single and multiple selections on a menu of options by using the HTML select element and option elements. To specify whether multiple options can be selected, use the multiple attribute. The size attribute can be used to specify how many options are visible at once. lightning-select also accepts most general form input attributes such as required and disabled.
Here’s an example on how to create a menu of options. Pass a default value to the value attribute to make it selected by default when the component loads.
1<lightning-select2 value={selectVal}3 label="Select a product"4 options={productOptions}5 onchange={handleChange}6></lightning-select>
In your JavaScript, define an array of options. Each option has a value and label property. The value is returned when you select an option. The label value is the text to show on the menu.
Selecting an option triggers the change event, which calls the
onchange handler. To check which option is selected,
use event.detail.value. If your value attribute is null, event.detail.value returns an empty string.
Design
lightning-select implements the select blueprint in the Salesforce Lightning Design System (SLDS). The component adapts to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
To enable multiple selection of options, use the multiple attribute. You can use the size attribute to specify the number of options to display by default.
1<lightning-select2 value={selectVal}3 label="Select a product"4 options={productOptions}5 onchange={handleChange}6 multiple7 size="3"8></lightning-select>
When you specify multiple, most browsers show a scrolling list box instead of a single-line dropdown. Alternatively, use the lightning-dual-listbox component to move options between two lists and reorder the list options.
Create Options
To create menu options, pass in the following properties to the options attribute.
Property
Type
Description
label
string
The text to show next to a checkbox.
value
string
The string that’s used to identify which checkbox is selected.
disabled
boolean
If true, the option isn’t selectable and users can’t interact with it.
Input Validation
Client-side input validation is available for this component. You can make
the selection required by adding the required attribute. An error message is
automatically displayed when an item isn’t selected and the element is required.
To check the validity states of an input, use the validity attribute, which
is based on the ValidityState object. You can access the validity states in
your JavaScript. This validity attribute returns an object with
boolean attributes.
You can override the default message by providing your own value for
message-when-value-missing.
Here’s an example that displays a custom field-level message when a user interacts with the component but doesn’t select an option.
1<lightning-select2 value={selectVal}3 label="Select a product"4 options={productOptions}5 onchange={handleChange}6 required7 message-when-value-missing="Which area can we help you with?"8>9</lightning-select>
Set the default value to an empty string so that an option isn’t selected by default.
When you click the Register button, the register() function shows a custom error message if no option is selected, or clears the error message if an option has been selected.
1import{LightningElement}from "lwc";23export default class CustomSelectError extends LightningElement{4 selectVal = "";5 productOptions = [6{7 label: "--None--",8 value: "",9},10{11 label: "Sales",12 value: "sales",13},14{15 label: "Marketing",16 value: "marketing",17},18{19 label: "Service",20 value: "service",21},22];2324 handleChange(event){25 this.selectVal = event.detail.value;26}2728 register(event){29 const selectCmp = this.template.querySelector("lightning-select");30 if(this.selectVal === ""){31 selectCmp.setCustomValidity("You have not selected an option");32}else{33 selectCmp.setCustomValidity("");34}35 // Display the error without user interaction36 selectCmp.reportValidity();37}38}
Component Styling
Use a combination of the variant and class attributes to customize the dropdown menu.
Variants
Use the variant attribute with one of these values to position the labels differently relative to the dropdown menu.
standard is the default, which displays the label above the dropdown menu.
label-hidden hides the label but make it available to assistive technology. If you provide a value for field-level-help, the tooltip icon is still displayed.
label-inline aligns the label and dropdown menu horizontally.
label-stacked places the label above the dropdown menu.
Utility Classes
To apply additional styling, use the SLDS utility classes with the class attribute. For example, you can add padding on the top of the component using the slds-p-top_medium SLDS class.
Styling Hooks
Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.
lightning-select uses delegatesFocus to manage focus. tabindex is not supported. See Handle Focus for more information.
Option groupings and disabled options are currently not supported.
lightning-select has usage differences from its Aura counterpart (lightning:select). lightning:select does not support multiple selection, and it takes in menu options as subcomponents in lightning:select. lightning:select doesn’t currently support multiple selection.
Alternatively, consider using lightning-combobox if your dropdown menu doesn’t need mobile support or multiple selection.
Accessibility
Provide a text label for accessibility using the label attribute, which creates an HTML <label> element for your input component.
To hide the label from view and make it available to assistive technology, use the label-hidden variant.
When the component is in an invalid state, an error message is displayed under the <select> element. The message is contained in a <div> element that specifies role="status".
To enable screen readers to announce the error message, the id value is associated with the aria-describedby attribute on the <select> element.
The label is also used as assistive text for screen readers.
On desktop, select multiple options by clicking an option and dragging up or down the list. Alternatively, hold the Ctrl, Command, or Shift keys
(depending on your operating system) and then click multiple options to select or deselect them.
For more information, see Selecting multiple options in the MDN web docs.
Validation errors are displayed below the dropdown menu using role="status". When an error is displayed, the select element updates the aria-describedby value to the ID of the element that contains the error message. The label value is also used as assistive text for screen readers.
Custom Events
change
The event fired when an option is selected.
The change event returns the following parameter.
Parameter
Type
Description
value
string
The value of the selected option.
The event properties are as follows.
Property
Value
Description
bubbles
true
This event bubbles up through the DOM.
cancelable
false
This event has no default behavior that can be canceled. You can’t call preventDefault() on this event.
composed
true
This event propagates outside of the component in which it was dispatched.
Attributes
Name
Description
Type
Default
Required
access-key
A shortcut key that activates and focuses on the menu.
string
aria-described-by
Aria Described by value on parent lighting-select
string
aria-labelled-by
A space-separated list of element IDs that provide labels for the aria-labelled-by value on parent lighting-select.
string
autocomplete
Reserved for internal use. Controls auto-filling of the field.
string
disabled
Specifies whether the menu is disabled and users cannot interact with it.
boolean
false
field-level-help
Help text detailing the purpose and function of the menu of options. The text is displayed in a tooltip above the menu.
string
label
The text label for the component. To hide the label but make it available to assistive technologies, use the label-hidden variant.
string
message-when-value-missing
The error message that's displayed below the menu when a user interacts with the menu but does not select an option.
string
multiple
Specifies whether multiple options can be selected.
boolean
false
name
The identifier for the component.
string
options
An array of menu options with key-value pairs for label and value.
Option[]
required
Specifies whether an option must be selected.
boolean
false
size
The number of rows in the list that should be visible at one time. Use this attribute with the multiple attribute.
number | string | null
4
validity
Represents the validity states that an element can be in, with respect to constraint validation.
ValidityState
value
The value of the selected option. If empty and a value is required, the component is in an invalid state.
string | string[]
variant
The variant changes the appearance of the dropdown menu. Accepted variants include standard, label-inline, label-hidden, and label-stacked. This value defaults to standard, which displays the label above the dropdown menu. label-hidden hides the label but make it available to assistive technology. label-inline horizontally aligns the label and dropdown menu. label-stacked places the label above the dropdown menu.
string
Methods
Name
Description
Argument Name
Argument Type
Argument Description
blur
Removes focus on from the select element.
checkValidity
Checks if the input is valid.
focus
Sets focus on the select element.
reportValidity
Displays the error messages and returns false if the input is invalid. If the input is valid, reportValidity() clears displayed error messages and returns true.
setCustomValidity
Sets a custom error message to be displayed when a form is submitted.
message
string
The string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalid
Displays an error message on an invalid select field. An invalid field fails at least one constraint validation and returns false when checkValidity() is called.