Lesson 23 of 36
HTML Tutorial

Textarea, Select and Datalist

Collect long text and pick-from-a-list choices with textarea, select, option and datalist.

3 min readBeginnerWeb FundamentalsHTML Forms

The <input> element handles single-line values. For longer messages and for choosing from lists, HTML provides three more controls: <textarea>, <select> and <datalist>.

textarea: multi-line text#

Use a textarea for messages, reviews and comments. Unlike <input>, it has opening and closing tags, and the text between them becomes the starting value.

html
<label for="message">Your message</label>
<textarea id="message" name="message" rows="5" cols="40" maxlength="500"
          placeholder="Tell us about your visit"></textarea>
  • rows and cols set the initial size in lines and characters. CSS (width, height) gives more precise control.
  • maxlength and minlength limit the length.
  • required, readonly and disabled work as usual.
  • wrap="soft" (default) wraps text visually, while wrap="hard" also inserts line breaks into the submitted value.

Whitespace inside the tags is preserved, so avoid indenting a pre-filled value, or it will include the spaces.

select and option: a drop-down list#

<select> creates a drop-down, and each choice is an <option>. The value is what gets submitted, while the text between the tags is what the user sees.

html
<label for="drink">Choose a drink</label>
<select id="drink" name="drink" required>
  <option value="">-- Please choose --</option>
  <option value="espresso">Espresso</option>
  <option value="latte" selected>Latte</option>
  <option value="chai">Masala chai</option>
</select>
  • selected makes an option the default.
  • An empty first option with value="" combined with required forces the user to make a deliberate choice.
  • disabled on an option makes it unselectable.

Groups and multiple selection#

html
<label for="snack">Snacks (hold Ctrl or Cmd to pick several)</label>
<select id="snack" name="snacks" multiple size="4">
  <optgroup label="Sweet">
    <option value="brownie">Brownie</option>
    <option value="cookie">Cookie</option>
  </optgroup>
  <optgroup label="Savoury">
    <option value="samosa">Samosa</option>
    <option value="sandwich">Sandwich</option>
  </optgroup>
</select>

<optgroup> organises options under a label, multiple allows more than one selection, and size sets how many rows are visible. Multi-select lists are awkward to use, so consider checkboxes instead when there are only a few choices.

datalist: suggestions with freedom#

A <datalist> supplies suggestions to a text input. The user can pick one or type something different.

html
<label for="city">Where are you visiting from?</label>
<input list="cities" id="city" name="city">
<datalist id="cities">
  <option value="Kathmandu">
  <option value="Pokhara">
  <option value="Lalitpur">
  <option value="Biratnagar">
</datalist>

The input's list attribute must match the datalist's id. Because typing is allowed, you must still validate the value on the server. Support for how suggestions are displayed varies between browsers, so do not rely on it for critical choices.

Which control should I choose?#

SituationControl
Long free texttextarea
One choice from a long listselect
One choice from 2 to 5 optionsRadio buttons (next lesson)
Free text with suggestionsinput with datalist

Common mistakes#

  • Indenting the pre-filled text of a textarea, adding unwanted spaces.
  • Forgetting value on options, so the visible text is submitted instead.
  • Missing name on the select or textarea.
  • Using select for just two options.
  • Treating datalist as a strict list.

Try it yourself#

Create a feedback form with a required drink select (with a placeholder option), a datalist for the visitor's city, and a textarea limited to 300 characters. Submit with a GET method temporarily and read the query string to see exactly which values were sent.

Recap#

textarea collects long text, select with option offers a fixed list, and datalist adds suggestions to a text input. Label them all, set meaningful values, and validate on the server.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-