# select, textarea & buttons

> select constrains a choice to a fixed list. datalist offers suggestions while leaving the field open. textarea accepts multi-line text. button has three type values that determine what happens when a user clicks it inside a form.

Subject: HTML | Lesson 28 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/select-textarea-and-buttons
Previous: [Checkboxes, Radios & Pickers](https://codewithanimation.in/html/checkbox-radio-and-pickers.md) | Next: [Grouping & Gauges](https://codewithanimation.in/html/fieldset-and-gauges.md)

## What it is

`<select>` creates a dropdown whose choices are `<option>` children. The `name` attribute on the `<select>` is the submission key; the `value` attribute on each `<option>` is the submitted value. If an `<option>` has no `value` attribute, its text content is submitted instead. `<optgroup label="...">` wraps related options under a non-selectable heading. Adding `multiple` and `size` turns the dropdown into a visible list box where the user can select more than one option.

`<datalist>` is often confused with `<select>` but works differently: it provides a suggestion list for a free-text input, not a constrained picker. Link an `<input>` to a `<datalist>` by matching the input's `list` attribute to the datalist's `id`. The user can pick from the suggestions or type any value they like. `<select>` forces a choice from the list; `<datalist>` merely hints.

`<textarea>` is the multi-line equivalent of `<input type="text">`. It is not a void element: it has an opening and closing tag, and the default value is whatever text sits between them (not a `value` attribute). The `rows` and `cols` attributes give it an initial size, but CSS is usually a better tool for sizing. The CSS `resize` property controls whether the user can drag the corner to resize it.

## Why it matters

Choosing between `<select>` and `<datalist>` changes the contract with the user. `<select>` is appropriate when the valid choices are a closed set that the server needs to validate against. `<datalist>` is appropriate when suggestions are helpful but the server accepts any value, such as a city field that pre-populates common names but also accepts cities not in the list. Using `<select>` where the user needs freedom creates friction; using `<datalist>` where only specific values are valid creates work for the server.

The `<button>` type default is the single most surprising form behaviour for developers new to HTML. A `<button>` inside a `<form>` with no `type` attribute behaves as `type="submit"` and submits the form on click. This causes accidental submissions when a button intended for JavaScript interaction (a "show more" toggle, a step counter) is placed inside a form without `type="button"`. Always set the type explicitly.

## How it works

For `<select>`, put the `name` on the select element and a meaningful `value` on each option. Use `<optgroup label="Group name">` to wrap related options: the label becomes a non-selectable separator in the dropdown. Add a first `<option value="" disabled selected>` to create a placeholder that forces the user to make a choice. Pair with `required` to prevent submission without a selection.

For `<datalist>`, give it a unique `id` and set the `list` attribute on the paired `<input>` to that id. Each `<option>` inside the datalist needs a `value` attribute (the string suggested). The `<option>` content between tags can also carry a human-readable label in some browsers, but the `value` is what fills the input. The input still has its own `name`, `type`, and validation attributes.

For buttons, the three `type` values map to three behaviours: `submit` submits the enclosing form (default); `button` does nothing by default and is used with JavaScript event listeners; `reset` returns every field in the form to its default value (rarely desirable, since users accidentally hit it). A `<button>` can contain any inline content, making it preferable to `<input type="submit">` when the button needs an icon alongside text.

## Common mistake: Forgetting type="button" and triggering an accidental form submission

Any `<button>` inside a `<form>` that does not set `type="button"` will submit the form when clicked. This is the default specified in HTML. A step counter, a collapsible panel toggle, or a "clear" action button placed inside a form without `type="button"` fires a submission the moment it is clicked. The fix is one attribute, but the symptom (a page reload and lost data) is confusing enough that it is worth making `type` explicit on every button in a form.

## Tip: Use <button> over <input type="submit"> whenever you need icons or styled content

`<input type="submit">` renders a plain text button whose label is its `value` attribute. You cannot put an icon inside it. `<button type="submit">` can contain any inline HTML: an icon component, a loading spinner, a badge count. Both submit the form on click, so there is no reason to choose the less capable input form unless you need to support very old browsers or are generating markup from a system that cannot produce button elements.

## Try it: Try it

Switch between select, datalist, and button modes to see how each controls choices and form behaviour.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/select-textarea-and-buttons

## Check yourself

### Question 1

A city field should offer auto-complete suggestions but also accept cities not in the list. Which element is correct?

- A. <datalist>: it suggests values but allows free text
- B. <select>: it provides a predefined list of cities
- C. Either works identically for free-text fields

Correct answer: A

`<datalist>` offers suggestions but does not restrict input. The user can type any city. `<select>` would prevent entering a city not already in the list.

- Why not B: <select> constrains the user to the options in the list. A city not in the list cannot be submitted. datalist is the right choice when the field is open but benefits from suggestions.
- Why not C: <select> does not accept free text. Only the options defined in the markup can be submitted. datalist provides suggestions while keeping the input open to any value.

### Question 2

A <button> inside a <form> has no type attribute. What happens when the user clicks it?

- A. The form submits, because the default type is submit
- B. Nothing happens, because a button without type has no default behaviour
- C. The browser shows a warning that type is missing

Correct answer: A

`<button>` defaults to `type="submit"`. Inside a form, it submits the form on click. Always add `type="button"` to buttons that should not trigger submission.

- Why not B: The HTML specification defines the default type of <button> as submit, not as inert. A button with no type attribute inside a form will submit the form on click.
- Why not C: Browsers do not warn about missing type attributes on buttons. They silently apply the default (submit), which is why accidental submissions happen without any visible error.

### Question 3

How do you set a default value for a <textarea>?

- A. Place the text between the opening and closing <textarea> tags
- B. Set a value attribute: <textarea value="default text" />
- C. Set the placeholder attribute, which also pre-fills the field

Correct answer: A

`<textarea>` is not a void element. Its default value is whatever text sits between the opening and closing tags: `<textarea>Default text here</textarea>`. The `value` attribute does not exist on textarea.

- Why not B: <textarea> does not use a value attribute for its initial content. It is not a void element. The text between the opening and closing tags becomes the default value.
- Why not C: placeholder is hint text shown when the field is empty. It does not pre-fill the field and disappears when the user starts typing. It is not a substitute for a default value.

## Remember this

- `<select>` constrains choice to its options; `<datalist>` suggests values but the field stays open for any input.
- An `<option>` with no `value` attribute submits its text content as the value.
- `<button>` defaults to `type="submit"` inside a form: always set `type="button"` for JS-only buttons.
- `<textarea>` default value lives between its tags, not in a `value` attribute.
