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.
<label for="message">Your message</label>
<textarea id="message" name="message" rows="5" cols="40" maxlength="500"
placeholder="Tell us about your visit"></textarea>rowsandcolsset the initial size in lines and characters. CSS (width,height) gives more precise control.maxlengthandminlengthlimit the length.required,readonlyanddisabledwork as usual.wrap="soft"(default) wraps text visually, whilewrap="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.
<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>selectedmakes an option the default.- An empty first option with
value=""combined withrequiredforces the user to make a deliberate choice. disabledon an option makes it unselectable.
Groups and multiple selection#
<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.
<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?#
| Situation | Control |
|---|---|
| Long free text | textarea |
| One choice from a long list | select |
| One choice from 2 to 5 options | Radio buttons (next lesson) |
| Free text with suggestions | input with datalist |
Common mistakes#
- Indenting the pre-filled text of a textarea, adding unwanted spaces.
- Forgetting
valueon options, so the visible text is submitted instead. - Missing
nameon theselector textarea. - Using
selectfor 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
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -