The select and option tag for dropdown Lists in HTML5 from which the user can choose one or more options. The <select> element is used to create a dropdown menu (also known as a select box or drop-down list) from which the user can choose one or more options. The options within the dropdown are defined by the <option> elements, which are nested inside the <select> element.
Attributes of <select> Element
- Container Element: The <select> element acts as a container for <option> elements.
- name Attribute: The name attribute identifies the dropdown list when the form is submitted, and the selected value of the <option> will be sent to the server.
- size Attribute: By default, a dropdown list shows one option at a time. The size attribute can be used to display a specific number of options as a scrolling list box. A size of 0 or 1 displays the standard dropdown style.
- multiple Attribute: When the multiple attributes is present, the user can select more than one option from the list ( by holding down the Shift or Ctrl/Cmd key while clicking).
- disabled Attribute: If the disabled attribute is set on the <select> element, the entire dropdown list is disabled, and users cannot interact with it.
- form Attribute: Similar to <textarea>, the form attribute (HTML5) allows the <select> element to be associated with a <form> even if it’s not within that form.
- autofocus Attribute: This HTML5 attribute specifies that the dropdown list should automatically receive focus when the page loads.
- required Attribute: This HTML5 attribute indicates that the user must select an option from the dropdown list before the form can be submitted.
Attributes of <option> Element
- Defines an Option: Each <option> element within a <select> element represents a choice in the dropdown list.
- value Attribute: The value attribute specifies the value that will be sent to the server when that option is selected. If the value attribute is not specified, the text content of the <option> tag is used as the value.
- Text Content: The text between the opening <option> and closing </option> tags is what the user sees in the dropdown list.
- selected Attribute: The selected attribute can be added to one or more <option> elements to indicate which option(s) should be pre-selected when the page loads.
- disabled Attribute: The disabled attribute can be used on individual <option> elements to make them unselectable.
- label Attribute: The label attribute can be used to specify a label for the option that might be used by user agents instead of the element’s text content. This is particularly useful for accessibility.
Example:
<form action="/submit" method="post">
<label for="favoriteFruit">Choose your favorite fruit:</label><br>
<select id="favoriteFruit" name="favoriteFruit">
<option value="apple">Apple</option>
<option value="banana" selected>Banana</option>
<option value="orange" disabled>Orange (Out of Stock)</option>
<option value="grape">Grape</option>
</select><br><br>
<label for="drinks">Select your preferred drinks (multiple allowed):</label><br>
<select id="drinks" name="drinks" multiple size="3">
<option value="water">Water</option>
<option value="juice">Juice</option>
<option value="soda" selected>Soda</option>
<option value="milk" selected>Milk</option>
<option value="tea">Tea</option>
<option value="coffee">Coffee</option>
</select><br><br>
<input type="submit" value="Submit Preferences">
</form>
In the first <select> example, a single selection dropdown list named “favoriteFruit” is created. “Banana” is pre-selected due to the selected attribute, and “Orange” is disabled. In the second example, a multi-select list named “drinks” is created, allowing the user to select multiple options. The size=”3″ attribute makes it display three options at a time with a scrollbar if there are more. “Soda” and “Milk” are pre-selected.
You can also group related options within a <select> element using the <optgroup> element to provide better organization in longer lists.
Example:
<label for="cars">Choose a car:</label><br>
<select id="cars" name="cars">
<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>
This structure helps in organizing the dropdown options visually for the user.
HTML5 Topics
- <div>
- <b>, <strong>, <u>, <em> and <i>
- <mark>, <small>, <del>, and <ins>
- <q>,<blockquote> and <cite>
- <dfn> and <pre>
- <abbr> and <code>
- Hyperlink or <a> tag
- <img> Tag and Attributes
- <figure> and <figcaption>
- <tr>,<td>,<th>,<tbody>,<thead>,<tfoot>, cplspan, rowspan and scope
- <form>, Action, Method-GET and POST