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.
1<aura:component>2 <aura:attribute name="colors" type="String[]" default="Red,Green,Blue" />3 <lightning:select4 name="select"5 label="Select a Color"6 required="true"7 messageWhenValueMissing="Did you forget to select a color?"8 >9 <option value="">-- None --</option>10 <aura:iteration items="{!v.colors}" var="color">11 <option value="{!color}" text="{!color}"></option>12 </aura:iteration>13 </lightning:select>14</aura:component>
Generating Options On Initialization
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 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.
1updateSelect: function(component, event, helper){2 var opts = [3{value: "Cyan", label: "Cyan"},4{value: "Yellow", label: "Yellow"},5{value: "Magenta", label: "Magenta", selected: true}];6 component.set('v.options', opts);7 //set the new selected value on the component8 component.set('v.selectedValue', 'Magenta');9 //return the selected value10 component.find("mySelect").get("v.value");11}
Input Validation
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.
1({2 handleSubmit: function(cmp){3 var select = cmp.find("options");4 if(!select.checkValidity()){5 select.showHelpMessageIfInvalid();6}else{7 alert("Ready to submit!");8}9},10});
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.
Usage Considerations
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.
The client-side controller updates the selected option by changing the v.status value, which triggers the change handler.
1({2 changeSelect: function(cmp, event, helper){3 //Press button to change the selected option4 cmp.find("select").set("v.value", "closed");5},6 handleChange: function(cmp, event, helper){7 //Do something with the change handler8 alert(event.getParam("value"));9},10});
Accessibility
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.
Attributes
Name
Description
Type
Default
Required
body
The body of the component. In markup, this is everything in the body of the tag.
Aura.Component[]
disabled
Specifies that an input element should be disabled. This value defaults to false.
Boolean
false
label
Text that describes the desired select input.
String
messageWhenValueMissing
Error message to be displayed when the value is missing.
String
name
Specifies the name of an input element.
String
onchange
The action triggered when a value attribute changes.
Aura.Action
readonly
Specifies that an input field is read-only. This value defaults to false.
Boolean
false
required
Specifies that an input field must be filled out before submitting the form. This value defaults to false.
Boolean
false
validity
Represents the validity states that an element can be in, with respect to constraint validation.
Object
value
Specifies the value of an input element.
Object
variant
The variant changes the appearance of an input field. Accepted variants include standard, label-inline, label-hidden, and label-stacked. This value defaults to standard, which displays the label above the field. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and input field. Use label-stacked to place the label above the input field.
String
standard
Methods
Name
Description
Argument Name
Argument Type
Argument Description
checkValidity
Returns the valid property value (Boolean) on the ValidityState object to indicate whether the select has any validity errors.
showHelpMessageIfInvalid
Shows the help message if the form control is in an invalid state.