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

A room selector should offer only the rooms that exist. A practice question may need several sentences. A topic field may suggest common topics while still accepting a new one.

NeedControlMain idea
Choose from provided optionsselect with optionA defined choice list
Write a longer answertextareaMulti-line text entry
Type freely with suggestionsinput with datalistSuggestions, not a closed choice list

Do not use datalist as though it proves the reader chose an approved option. If the answer must be from a closed set, a select and receiving-side checks are often more appropriate.

Select and option

Select is the control that holds the chosen value. Option is one entry. Give select an id for its label and a name for submission. Give each option a useful value.

HTML fragment — part of a document
<label for="room">Practice room</label>
<select id="room" name="room">
  <option value="room6">Room 6</option>
  <option value="room8">Room 8</option>
</select>

If Room 8 is chosen, the data is room=room8. The visible label and the submitted value can differ. If value is absent, the option's text is used, but an explicit value keeps the data intention clear.

Selected marks an initial option choice. It is boolean. Without an explicit selection, an ordinary single-select dropdown commonly selects its first eligible option. If a deliberate choice is required, use a suitable initial empty-value prompt and required, rather than silently treating the first room as the student's answer.

Textarea holds more than one line

Textarea needs opening and closing tags. Its initial text is written between those tags, not in a value attribute. Newlines and spaces in that text can become part of the initial value, so avoid accidentally filling it with indentation.

Rows and cols suggest the control's initial visible size. They do not limit the number of submitted lines or characters. Maxlength can constrain user-entered length, and CSS can alter the display size.

Complete program: practice-request.html

Save form-result.html from Chapter 31 beside this file. This remains a local practice submission rather than a real request service.

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>
Browser previewDocument title: A Room Choice and a Longer Question

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.

Expected result: Room 8 starts selected and the textarea contains a question. Change both, add a second line and inspect a local submission's room and question entries. The receiving static page does not save the answer.

Grouping and selecting several options

Optgroup gathers related options and supplies a label. It is not itself a selectable answer. Put options inside it, and avoid nesting optgroup elements.

HTML fragment — part of a document
<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>

Multiple allows several options. Selected choices produce repeated entries for the select's name. Selection gestures vary: desktop systems may use modifier keys, and touch interfaces may present another picker. Provide clear instructions and consider checkboxes when a small set of independent options would be easier.

Size on select suggests the number of visible rows. It is unrelated to a text input's character-width size and a textarea's rows/cols. Identical attribute names can have element-specific meanings.

Datalist suggests without restricting

An input's list attribute refers to a datalist id. The input owns the submitted name and current value; the datalist provides suggestions. A value outside those suggestions can still be allowed.

Match the expected answer
  1. 1Select: choose from the provided options
  2. 2Textarea: enter several lines
  3. 3Datalist: suggest values for a text input
  4. 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

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>
Browser previewDocument title: Suggest a Topic Without Closing the List

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.

Expected result: a labelled text field. Depending on the browser, typing or opening its suggestion interface offers the prepared topics. Type Images even though it is not listed; the datalist alone does not reject that value. Suggestions and their accessibility support vary, so keep the field usable without discovering a dropdown.

Other states and useful limits

Select and textarea can have name, disabled and required where appropriate. Readonly applies to textarea but not select. A disabled select is not a way to show an unchangeable value while still submitting it: disabled controls are excluded.

For an optional select value, think about what no answer should mean. For a required normal single dropdown, a first direct option with value="" can act as a prompt. Leaving that prompt chosen does not satisfy required. We will test this in the validation chapter.

Textarea wrap="soft" controls visual wrapping without adding layout-based line breaks to the submitted value. Hard wrapping has different submission rules and requires cols. Keep the default soft behaviour for ordinary questions unless hard-wrapped text is genuinely required.

Problem and solution: a textarea starts empty unexpectedly

Problem: a student writes textarea value="My question" and leaves its content empty.

Solution: put My question between the opening and closing textarea tags. Textarea is not an input with a value attribute. Then reload the page and check the initial text, including accidental whitespace.

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

  1. Which control suits a fixed room list?
  2. Where does textarea's starting text belong?
  3. How do selected and checked differ in these lessons?
  4. What does multiple on select allow, and how are its chosen values submitted?
  5. Can datalist alone prevent a new unlisted topic?
  6. 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
  1. A labelled select with suitable options and receiving-side checks for a real service.
  2. Between its opening and closing tags.
  3. Selected marks an option's initial choice; checked marks a checkbox or radio's initial state.
  4. Several selected options, usually as repeated entries under the select's name.
  5. No. It supplies suggestions rather than a closed-set rule.
  6. 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?

Select offers an option-based control. Datalist supplies suggestions to another input that can generally accept other valid values. Choose based on whether the answer is meant to be restricted.

Do textarea dimensions constrain its content length?

No. Rows and cols describe a visible size. Length constraints and the receiving application's limits are separate concerns.

Why can a disabled select's value disappear from submission?

Disabled controls are excluded from ordinary form data. Readonly is not supported as an equivalent select setting, so design the interface and receiving logic appropriately.

Are native choice lists identical across devices?

No. Presentation, gestures and suggestion support can vary. Keep labels and instructions clear, and test the environments your students use.

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.