# `<form>`

> The `<form>` HTML element represents a collection of form controls (such as `input`, `textarea`, `button`, `select`, etc.) for submitting data to a server.

## How do I build an accessible HTML form that submits correctly?

The element that is used to collect user input through various controls like text fields, checkboxes and buttons. The data entered in a form can be sent to a server for processing, making it a key element in user interaction on the web.

- Topic: Forms
- Type: tag
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 4+, Safari iOS 3.2+

## Example

Wrong:

```js
button.addEventListener("click", () => {
  send(new FormData(form));
});
```

Right:

```js
form.addEventListener("submit", (event) => {
  event.preventDefault();
  send(new FormData(form));
});
```

The `submit` event fires for the Enter key, button clicks and `requestSubmit()`, and only after validation passes.

## Common mistakes

- Forgetting `name` on inputs, so the server receives empty data.
- Listening to the button's `click` instead of the form's `submit`, which breaks Enter to submit.

## Key points

- Every control has a `name`, so its value is submitted.
- Every control has a visible `<label>` linked with `for` and `id`.
- Forms that change data use `method='post'`, not the default 'get'.
- Submission is handled on the form's `submit` event, not on the button's `click`.

## Interview questions

### What is the default method of a form?

The default is 'get', which puts values in the URL. Use 'post' for sensitive data or changes.

### Why does my form reload the page?

Submitting navigates to the `action` URL by default. Call `event.preventDefault()` in the `submit` handler to handle it with JS.

## Related cards

- [`<input>`](https://wonderium.dev/html/input.md)
- [`<label>`](https://wonderium.dev/html/label.md)
- [FormData](https://wonderium.dev/js/form-data.md)
- [Accessible Forms](https://wonderium.dev/a11y/accessible-forms.md)
- [Actions](https://wonderium.dev/react/actions.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/form
- Last updated: 2026-11-25
