form element
Groups fields and handles submission.
Forms collect structured input. JavaScript can enhance them, but HTML gives forms their foundation.
form.addEventListener("submit", event => {
event.preventDefault();
const data = new FormData(form);
});
Events & Forms
A form is more than a group of inputs. It has submission behavior, validation, labels, names and browser support built in.
JavaScript can intercept submit, read values with FormData and show custom feedback without abandoning the native form model.
Every input that should be submitted needs a name. Every visible input should have a label.
Groups fields and handles submission.
Connects text to a form control.
Defines the key used in submitted data.
Reads form values as key/value pairs.
Examples
<label>Title <input name="title"></label>
const data = new FormData(form);
const title = data.get("title");
<input placeholder="Title"> // No label, no name, weak accessibility and weak submission.
Code patterns
These examples are the event patterns you will reuse constantly: listen, inspect, prevent, delegate, validate and submit.
Handle form submission.
form.addEventListener("submit", event => {
event.preventDefault();
});
Collect named field values.
const data = new FormData(form);
const title = data.get("title");
Return fields to default values.
form.reset();
Use a real submit button.
<button type="submit">Save</button>
Rules that matter
Events are user-facing code. Good handlers stay small, respect native behavior and keep visual, semantic and data state aligned.
They provide browser behavior for free.
Labels improve usability and accessibility.
FormData depends on name attributes.
Do not handle only button clicks.
Placeholders disappear and are not enough.
A form can still have a real action later.
Production thinking
Forms are a built-in structured-data system. Ignoring that means rebuilding weaker behavior by hand.
Labels, fieldsets, errors and focus behavior are essential for form usability.
Production forms should validate, submit, show errors and recover from failures predictably.
Forms do not usually drive SEO directly, but accessible forms improve user success and trust.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain what fires, where it fires and what default behavior remains, you understand the interaction.