Skip to content
Laviyx Learn

Try

HTML course menu

HTML select, option, optgroup, datalist and textarea

Learn the HTML select element for drop-down lists, option values and selected, grouping options with optgroup, multiple selection, suggestions with datalist, and multi-line text with textarea.

Intermediate6 min readUpdated
On this page

Besides <input>, forms use three more elements: <select> for choosing from a list, <datalist> for suggestions, and <textarea> for longer text.

HTML
<form action="/register" method="post">
  <label for="course">Choose a course</label>
  <select id="course" name="course">
    <option value="">-- Please choose --</option>
    <option value="html">HTML</option>
    <option value="css" selected>CSS</option>
    <option value="js">JavaScript</option>
  </select>
  <button>Register</button>
</form>
Result
  • Each <option> is a choice. Its value is what is submitted; its text is what the user sees.
  • selected chooses the starting option. Without it, the first option is selected.
  • A first option with an empty value, like "Please choose", combined with required, forces the user to make a real choice (see Form Validation).

Grouping options#

<optgroup> adds a heading inside long lists:

HTML
<label for="city">City</label>
<select id="city" name="city">
  <optgroup label="Telangana">
    <option>Hyderabad</option>
    <option>Warangal</option>
  </optgroup>
  <optgroup label="Karnataka">
    <option>Bengaluru</option>
    <option>Mysuru</option>
  </optgroup>
</select>
Result

When an <option> has no value attribute, its text is submitted.

Selecting several options#

multiple lets users choose more than one option (with Ctrl or Cmd), and size sets how many rows are visible:

HTML
<label for="skills">Skills</label><br>
<select id="skills" name="skills" multiple size="4">
  <option>HTML</option>
  <option>CSS</option>
  <option>JavaScript</option>
  <option>SQL</option>
</select>
Result

Multiple-select lists are awkward to use on many devices; a group of checkboxes is often clearer.

Suggestions: datalist#

<datalist> offers suggestions for a text field while still letting the user type anything. Connect it with the input's list attribute:

HTML
<label for="browser">Your browser</label>
<input id="browser" name="browser" list="browsers">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Edge">
  <option value="Firefox">
  <option value="Safari">
</datalist>
Result

Click the field or start typing to see matching suggestions.

UseWhen
<select>The answer must be one of a fixed list
<input list> + <datalist>Common answers exist, but anything is allowed

Multi-line text: textarea#

<textarea> is for longer text such as messages and comments. Unlike <input>, it has a closing tag, and its starting text goes between the tags:

HTML
<label for="message">Your message</label><br>
<textarea id="message" name="message" rows="5" cols="40" maxlength="500"
  placeholder="Tell us how we can help"></textarea>
Result
  • rows and cols set the visible size in lines and characters. CSS width and height are more common in real sites.
  • Users can usually resize a textarea by dragging its corner; CSS resize controls this.
  • Whitespace between the tags is part of the value, so write <textarea></textarea> with nothing in between unless you want starting text.

Practice#

Practice exercise

Create a feedback form with a required "Rating" drop-down (Excellent, Good, Average, Poor, with a "Choose a rating" placeholder option), a textarea for comments, and a submit button.

Show solution
HTML
<form action="/feedback" method="post">
  <p>
    <label for="rating">Rating</label>
    <select id="rating" name="rating" required>
      <option value="">Choose a rating</option>
      <option value="5">Excellent</option>
      <option value="4">Good</option>
      <option value="3">Average</option>
      <option value="2">Poor</option>
    </select>
  </p>
  <p>
    <label for="comments">Comments</label><br>
    <textarea id="comments" name="comments" rows="4" cols="40"></textarea>
  </p>
  <button type="submit">Send feedback</button>
</form>
Result

Try submitting without choosing a rating: the browser asks you to select one.

Key takeaway#

Finished this lesson?