Select

lightning:select

Represents a select input.

For Aura components only. For LWC development, use lightning-select.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

A lightning:select component creates an HTML select element. This component uses HTML option elements to create options in the dropdown list, enabling you to select a single option from the list. Multiple selection is currently not supported. To support multiple selection, use lightning:dualListbox instead.

This component implements styling from select in the Lightning Design System.

You can define a client-side controller action to handle various input events on the dropdown list. For example, to handle a change event on the component, use the onchange attribute. Retrieve the selected value using cmp.find("selectItem").get("v.value").

You can use aura:iteration to iterate over a list of items to generate options. This example iterates over a list of items.

Use an attribute to store and set the array of option value on the component. The following component calls the client-side controller to create options during component initialization.

In your client-side controller, define an array of options and assign this array to the items attribute.

In cases where you're providing a new array of options on the component, you might encounter a race condition in which the value on the component does not reflect the new selected value. For example, the component returns a previously selected value when you run component.find("mySelect").get("v.value") even after you select a new option because you are getting the value before the options finish rendering. You can avoid this race condition by binding the value and selected attributes in the lightning:select component as illustrated in the previous example. Also, bind the selected attribute in the new option value and explicitly set the selected value on the component as shown in the next example, which ensures that the value on the component corresponds to the new selected option.

Client-side input validation is available for this component. Set required="true" to make the dropdown menu a required field. If you interact with the menu without making a selection, an error message "Complete this field" is displayed on blur. To override the default message, provide your own value with the messageWhenValueMissing attribute.

If you don't interact with the required field, the blur event doesn't fire and the error message doesn't automatically display. To programmatically display an error when the field is invalid, use the checkValidity() and showHelpMessageIfInvalid() methods.

checkValidity() indicates whether the field has any validity errors. Alternatively, use select.get('v.validity').valid. The validity attribute is based on the HTML ValidityState object. The validity attribute returns an object with boolean properties like valid and valueMissing. If the value is missing on a required field, select.get('v.validity').valid returns false and select.get('v.validity').valueMissing returns true.

The Lightning web component equivalent for lightning:select is lightning-combobox. For more information, see the lightning-combobox documentation.

The onchange event is triggered only when a user selects a value on the dropdown list with a mouse click, which is expected behavior of the HTML select element. Programmatic changes to the value attribute don't trigger this event, even though that change propagates to the select element. To handle this event, provide a change handler for value.

This example creates a dropdown list and a button that when clicked changes the selected option.

The client-side controller updates the selected option by changing the v.status value, which triggers the change handler.

You must provide a text label for accessibility to make the information available to assistive technology. The label attribute creates an HTML label element for your input component. To hide a label from view and make it available to assistive technology, use the label-hidden variant.