pub struct Select;Expand description
The <select> element - represents a control for selecting among a set of options.
§Purpose
The <select> element provides a dropdown list of options from which users can choose
one or more values. Contains <option> elements that define the available choices, and
can be grouped using <optgroup> elements. More compact than radio buttons for multiple
choices.
§Content Categories
- Flow Content
- Phrasing Content
- Interactive Content
- Palpable Content
§Permitted Content Model
- Zero or more
<option>or<optgroup>elements
§Common Use Cases
- Country or state selection dropdowns
- Category or type selectors
- Quantity or size pickers
- Date component selectors (month, year)
- Multi-select lists for tags or categories
§Key Attributes
name: Name for form submissionmultiple: Allow selecting multiple optionssize: Number of visible optionsrequired: Make selection mandatorydisabled: Disable the entire selectautocomplete: Autocomplete hintform: Associates with a form by ID
§Example
<!-- Basic dropdown -->
<label for="country">Country:</label>
<select id="country" name="country">
<option value="">Select a country</option>
<option value="us">United States</option>
<option value="uk">United Kingdom</option>
<option value="ca">Canada</option>
</select>
<!-- Select with pre-selected option -->
<label for="size">Size:</label>
<select id="size" name="size">
<option value="s">Small</option>
<option value="m" selected>Medium</option>
<option value="l">Large</option>
</select>
<!-- Multi-select -->
<label for="interests">Interests (select multiple):</label>
<select id="interests" name="interests" multiple size="4">
<option value="sports">Sports</option>
<option value="music">Music</option>
<option value="art">Art</option>
<option value="tech">Technology</option>
</select>
<!-- Grouped options -->
<label for="car">Choose a car:</label>
<select id="car" name="car">
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
<!-- Required select with placeholder -->
<label for="department">Department:</label>
<select id="department" name="department" required>
<option value="" disabled selected>-- Choose department --</option>
<option value="sales">Sales</option>
<option value="engineering">Engineering</option>
<option value="support">Support</option>
</select>
<!-- Disabled select -->
<label for="status">Status:</label>
<select id="status" name="status" disabled>
<option>Processing</option>
</select>§Accessibility
- Always provide an associated
<label> - Use first option as a prompt/placeholder, not a valid choice
- Provide clear option text
- Group related options with
<optgroup> - For long lists, consider searchable alternatives
- Ensure keyboard navigation works properly
- Use
aria-describedbyfor additional instructions
§WHATWG Specification
Trait Implementations§
Source§impl HtmlElement for Select
impl HtmlElement for Select
impl CanContain<Optgroup> for Select
impl CanContain<Option_> for Select
impl CanContain<Script> for Select
impl CanContain<Template> for Select
impl FlowContent for Select
impl InteractiveContent for Select
impl PalpableContent for Select
impl PhrasingContent for Select
Auto Trait Implementations§
impl Freeze for Select
impl RefUnwindSafe for Select
impl Send for Select
impl Sync for Select
impl Unpin for Select
impl UnwindSafe for Select
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more