# Forms & User Input

> Master the intricacies of user input. Learn the critical difference between input and change events, and how to safely extract data on submit.

Subject: JavaScript | Lesson 45 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/forms-and-input
Previous: [Bubbling, Capturing & Delegation](https://codewithanimation.in/javascript/event-bubbling-delegation.md) | Next: [ES Modules: import & export](https://codewithanimation.in/javascript/modules-esm.md)

## The Gateway to Data

Forms are the primary way users send data to your application.

JavaScript provides specialized events specifically designed for reading text boxes, checkboxes, and form submissions.

## Input vs Change

When a user types into a text field, you have two primary ways to listen to them:

1. The `input` event: Fires immediately on **every single keystroke**. Use this for live search filtering or real-time character counters.

2. The `change` event: Fires only when the user **commits** the change, usually by clicking outside the input box (losing focus) or pressing Enter. Use this for heavy validations that shouldn't run on every keystroke.

## Handling the Submit

When a user clicks a Submit button, the `submit` event fires on the `<form>` element itself, *not* the button.

**Step 1:** You must call `e.preventDefault()` immediately to stop the browser from triggering a hard page refresh.

**Step 2:** Extract the data. You can read individual inputs using `input.value`, but modern JavaScript provides the `FormData` API. `const data = new FormData(formElement)` automatically bundles every named input in your form into a neat object.

## Common mistake: The Name Attribute

If you use the `FormData` API and it comes back empty, you probably forgot your HTML `name` attributes!

The browser uses the `name` attribute (e.g., `<input name="email">`) as the key when bundling form data. An `id` is not enough.

## Tip: Reading Checkboxes

Text inputs use `input.value`, but checkboxes and radio buttons are different. To see if a checkbox is ticked, you must read the boolean property `input.checked`.

## Try it: The Form Inspector

Type into the input fields and click submit. Watch the terminal to see exactly when the `input`, `change`, and `submit` events fire, and how `FormData` extracts the values.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/forms-and-input

## Check yourself

### Question 1

If you want to build a real-time 'characters remaining' counter as the user types, which event should you listen to?

- A. The 'input' event.
- B. The 'change' event.
- C. The 'keyup' event.

Correct answer: A

The `input` event is guaranteed to fire instantly whenever the value of the field changes, regardless of whether it was typed, pasted, or dictated.

- Why not B: Incorrect. 'change' won't fire until the user finishes typing and clicks away.
- Why not C: Incorrect. While 'keyup' works, it misses inputs from copy-pasting via the mouse. 'input' catches everything.

### Question 2

Why must you call `e.preventDefault()` inside a form's 'submit' listener in a modern single-page application?

- A. To stop the browser from performing a hard page reload.
- B. To clear the text inputs.
- C. To stop the event from bubbling up to the document.

Correct answer: A

Historically, forms sent a network request directly to a server which returned a completely new HTML page. In modern apps, JS intercepts the data to send an API request in the background instead.

- Why not B: Incorrect. You clear inputs manually using form.reset().
- Why not C: Incorrect. That is e.stopPropagation().

### Question 3

What HTML attribute is strictly required for the `FormData` API to automatically extract an input's value?

- A. The 'name' attribute.
- B. The 'id' attribute.
- C. The 'class' attribute.

Correct answer: A

The `FormData` API loops through all inputs inside the form and pairs the HTML `name` attribute with the current `value` to build its dataset.

- Why not B: Incorrect. IDs are for CSS and querySelector, not for form data transmission.
- Why not C: Incorrect. Classes are for styling.

## Remember this

- The `input` event fires on every keystroke; `change` fires when the input loses focus.
- The `submit` event fires on the `<form>`, not the button.
- Always call `e.preventDefault()` on a submit event to prevent a page refresh.
- Use the modern `new FormData(formElement)` API to easily extract all form data at once.
- Inputs must have a `name` attribute to be captured by `FormData`.
