Select

Struct Select 

Source
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 submission
  • multiple: Allow selecting multiple options
  • size: Number of visible options
  • required: Make selection mandatory
  • disabled: Disable the entire select
  • autocomplete: Autocomplete hint
  • form: 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-describedby for additional instructions

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Select

Source§

const TAG: &'static str = "select"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl CanContain<Optgroup> for Select

Source§

impl CanContain<Option_> for Select

Source§

impl CanContain<Script> for Select

Source§

impl CanContain<Template> for Select

Source§

impl FlowContent for Select

Source§

impl InteractiveContent for Select

Source§

impl PalpableContent for Select

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.