CHAPTER 34 · HTML
Lists and longer answers: select, textarea and datalist
Some questions need a fixed list of choices; others need several lines of writing or a suggested answer that can be changed. Choose between these controls by the kind of answer you really need.
Download example files (ZIP)Three familiar situations
Select and option
<label for="room">Practice room</label>
<select id="room" name="room">
<option value="room6">Room 6</option>
<option value="room8">Room 8</option>
</select>Textarea holds more than one line
Complete program: practice-request.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>A Room Choice and a Longer Question</title>
</head>
<body>
<h1>Describe your practice question</h1>
<form action="form-result.html" method="get">
<p><label for="room">Practice room</label></p>
<p><select id="room" name="room">
<option value="room6">Room 6</option>
<option value="room8" selected>Room 8</option>
</select></p>
<p><label for="question">Question to discuss</label></p>
<p><textarea id="question" name="question" rows="4" cols="28">How do two pages link together?</textarea></p>
<button type="submit">Inspect the practice request</button>
</form>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.
Grouping and selecting several options
<select id="topics" name="topics" multiple size="4">
<optgroup label="Text">
<option value="headings">Headings</option>
<option value="paragraphs">Paragraphs</option>
</optgroup>
<optgroup label="Navigation">
<option value="links">Links</option>
<option value="paths">File paths</option>
</optgroup>
</select>Datalist suggests without restricting
- 1Select: choose from the provided options
- 2Textarea: enter several lines
- 3Datalist: suggest values for a text input
- 4Name: identify the submitted value
Suggestions do not create a closed-set rule. Keep visible size and content constraints separate.
Complete program: suggested-topic.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Suggest a Topic Without Closing the List</title>
</head>
<body>
<h1>Suggest what to practise next</h1>
<p>Choose a suggestion or type another topic. You can type directly even if suggestions are not shown.</p>
<form action="form-result.html" method="get">
<p><label for="topic">Practice topic</label></p>
<p><input type="text" id="topic" name="topic" list="topic-suggestions"></p>
<datalist id="topic-suggestions">
<option value="Headings"></option>
<option value="Links"></option>
<option value="Tables"></option>
</datalist>
<button type="submit">Inspect the suggested topic</button>
</form>
</body>
</html>This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.
Other states and useful limits
Problem and solution: a textarea starts empty unexpectedly
Common mistakes and useful tips
Forgetting select's name and expecting a submitted choice. Treating option text and value as necessarily identical. Using checked on option instead of selected. Treating optgroup as a selectable answer. Expecting rows or cols to enforce maximum length. Assuming datalist forces an answer from its suggestions. Adding readonly to select and expecting it to work like a text field.
Practice questions
Which control suits a fixed room list? Where does textarea's starting text belong? How do selected and checked differ in these lessons? What does multiple on select allow, and how are its chosen values submitted? Can datalist alone prevent a new unlisted topic? Save the two complete programs and the destination page. Try a different room, a two-line question and an unlisted suggested topic, then inspect their named values.
Check your practice answers
A labelled select with suitable options and receiving-side checks for a real service. Between its opening and closing tags. Selected marks an option's initial choice; checked marks a checkbox or radio's initial state. Several selected options, usually as repeated entries under the select's name. No. It supplies suggestions rather than a closed-set rule. The room uses its selected option's value, the question uses textarea's text, and the topic uses the input's current text even when it is not one of the suggestions.
Interview questions
How does select differ from datalist?
Do textarea dimensions constrain its content length?
Why can a disabled select's value disappear from submission?
Are native choice lists identical across devices?
Quick revision
Select owns the chosen option value; optgroup groups related options. Textarea stores initial content between its tags. Datalist supplies suggestions to an input. Visible size, allowed values and submission rules are different decisions.