Textarea, Select and Datalist

Beginner · 10 min

How textarea, select and datalist collect multi-line text, a choice from a fixed list, and free text with suggestions, and what each one submits and validates.

Before thisInput Types

eazylearntech · 10 min

Textarea, Select and Datalist

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by eazylearntechReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<textarea>` is a separate element, not an input type. Its default text goes between the tags, and it takes `rows`, `cols` and `maxlength`. In a test, a newline right after the opening tag was dropped.
  • `<select>` forces a choice from a fixed list. The submitted value is the option's `value`, or its text if there is no `value` attribute.
  • A required `<select>` needs a first `<option value="">` as a placeholder. Without one, the first real option is selected from the start and the field is always valid.
  • `<datalist>` is only a list of suggestions for an `<input list="id">`. The user can still type anything: in a test, a value not on the list was valid.
  • Use `<select>` when the answer must be one of the options, `<datalist>` when the options are examples, and radio buttons when there are few enough options to show them all.

Three controls for three jobs

<input> handles a single line of text or a toggle. Three other elements cover cases it cannot.

  • <textarea>: several lines of free text, such as a message or a bio.
  • <select>: one (or several) choices from a list the author fixes, such as a country.
  • <datalist>: a set of suggestions that an ordinary input offers as you type, such as city names, where other answers are still allowed.

Picking the right one is half the work. The wrong choice is usually the same mistake: a dropdown where the answer can be anything, or free text where it must be one of a few values.

textarea

<label for="msg">Message</label>
<textarea id="msg" name="msg" rows="4" maxlength="500"></textarea>

A textarea is its own element, so its default content goes between the tags, not in a value attribute. In a test, <textarea> followed by a newline and first had the value first: the parser drops one leading newline. rows sets the visible height, and CSS (width, height, resize) is the better way to size it.

maxlength stops typing at the limit. In a test with maxlength="5", typing abcdefgh left abcde, and pressing Enter at the limit added nothing, because a newline is a character too. On submit, newlines are sent as CRLF, shown as %0D%0A, as the forms lesson shows. Strip \r on the server if you compare line by line.

select

<label for="country">Country</label>
<select id="country" name="country" required>
  <option value="">Choose a country</option>
  <option value="gb">United Kingdom</option>
  <option value="in">India</option>
</select>

The submitted value is the option’s value. If an option has no value, it is the option’s text. In a test, <option>Plain text</option> submitted Plain+text. Write explicit values: the visible text is for people and can be translated, and the value is for your server.

The placeholder option. A <select> always has a selected option, so with no placeholder, the first real option is chosen automatically and required has nothing to catch. A first <option value=""> fixes it: an empty value counts as missing. In a test, selecting that option gave validity.valueMissing of true, and picking another option made it false.

Other features: multiple turns it into a list where the user can pick several, and each picked option is sent as its own name and value pair. <optgroup label="Europe"> groups options under a heading. selected on an option sets the default.

A newer development is customizable select. The appearance: base-select CSS property, together with <selectedcontent>, lets you style the drop-down itself. According to web-features 3.47.0, it ships in Chrome 135 and Safari 27 but not Firefox, and is not Baseline, so treat it as an enhancement over a normal <select>, not a replacement.

datalist

A datalist is a list of suggestions, tied to an input by list:

<label for="fruit">Fruit</label>
<input id="fruit" name="fruit" list="fruits">
<datalist id="fruits">
  <option value="Apple">
  <option value="Banana">
</datalist>

As the user types, the browser shows matching entries. The user can pick one, or ignore them and type anything. That second part trips people up. In a test, a value of Zebra, which is not in the list, left the input valid. A datalist is a hint, not a constraint, so it cannot replace a <select> when the answer must be one of the options. If you need only listed values, validate with JavaScript or the server, or use a <select>.

It works with several input types, including text, email, url, number and range. Support is uneven: according to web-features it is not marked Baseline as a whole, and Firefox has no datalist for date, time or color inputs. The suggestion list is drawn by the browser, so you cannot style it much.

Choosing between them

  • A few options (up to about five) where the user benefits from seeing them all: radio buttons or checkboxes, grouped in a fieldset.
  • A longer list where the answer must be one of the options: <select>.
  • Open answers where you want to help the user: <input list> with a <datalist>.
  • Anything longer than a line: <textarea>.

What the video covers

The embed is the whole of eazylearntech’s “Select Datalist Textarea | HTML Crash Course for Beginners” (May 2024), the fifth part of a series. Its description says it covers dropdown menus with select, auto-complete with datalist, and multi-line text fields with textarea. It is a small channel and the video has no chapters, so I picked it for covering exactly these three elements. I could not read a transcript, so I do not describe what it shows. The parser behavior, the required placeholder trick, the datalist not constraining input and the submitted values are the lesson’s own and were tested in headless Chromium, Firefox and WebKit (the submitted-value test ran in Chromium only). I did not test how browsers draw the suggestion list. The runner-up is a short deep dive on datalist.

Where this fits

This follows input types and comes before fieldset, legend and grouping. The required behavior is revisited in built-in form validation.

Check yourself

  1. 01You write `<textarea>\nHello</textarea>` with the text on a new line after the tag. What is the textarea's value?

    `Hello`, without the leading newline. The HTML parser drops a newline that comes straight after `<textarea>`, so you can start the content on its own line for readability. A second blank line would be kept.

  2. 02A `<select required>` lists Alpha, Beta and Gamma. The user submits without touching it. Why does the form submit?

    The first option, Alpha, is selected by default, so the select already has a value and the required check passes. To force a choice, add `<option value="">Choose one</option>` first. An empty value counts as missing. In a test, that placeholder option made `validity.valueMissing` true.

  3. 03An option is written `<option>Plain text</option>` with no `value`. What gets submitted?

    The option's text, `Plain text`. A `value` attribute overrides it, which is why you usually write one: the text can change with translation or wording while the value stays stable for the server.

  4. 04A `<datalist>` lists Apple and Banana. A user types Zebra into the linked input. Is the form valid?

    Yes. A datalist only offers suggestions and does not restrict the input. In a test, `Zebra` made the input's `validity.valid` true. If only listed values are allowed, use a `<select>` or check the value yourself.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark