HTML <select> Element

Publish in HTML Tutorial el 23/05/2025 19:31

The <select> element creates a dropdown list that allows users to select one or multiple options from a list. It's commonly used in forms to collect user input when there are predefined choices available. The <select> element works with <option> elements to define the available choices, and optionally with <optgroup> to group related options.


Basic Syntax

Here's the basic structure of a <select> element:


Rendered Example

This is how the above code would appear in a browser:

Play Code


Examples


1. Basic Select with Default Value

A simple dropdown with a preselected option using the 'selected' attribute.

Play Code


2. Grouped Options with <optgroup>

Using <optgroup> to categorize options for better user experience.

Play Code


3. Multiple Selection

Adding the 'multiple' attribute allows users to select more than one option (usually with Ctrl/Cmd click).

Play Code


4. Select with JavaScript Event

This example shows how to use JavaScript to respond to selection changes.

Play Code


5. Dynamic Select with JavaScript

This example demonstrates how to populate a select element dynamically using JavaScript.

Play Code


Tips and Tricks

  • Always include a label for your select element for accessibility purposes.
  • Use the 'required' attribute to make selection mandatory in forms.
  • For long option lists, consider using the 'size' attribute to show multiple options at once.
  • Group related options with <optgroup> to improve usability.
  • When using multiple selection, provide clear instructions on how to select multiple items.
  • For very long lists, consider using a searchable select component (like Select2 or similar libraries).

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...