HTMLFormElement
Form Element
Represents a <form> tag. Groups inputs together and handles submission. You usually intercept the submit event to process data with JS instead of letting the browser navigate.
<form
action="/submit"
method="post"
></form>
Key Properties
| Property | Type | Description |
|---|---|---|
| elements | HTMLFormControlsCollection | All input/select/textarea elements inside the form. |
| action | string | URL where the form data is sent on submit. |
| method | string | "get" or "post". Controls how data is sent. |
Common Methods
| Method | Returns | Description |
|---|---|---|
| submit() | void | Submits the form programmatically (bypasses the submit event). |
| reset() | void | Clears all fields back to their default values. |
Quick Examples
JS
const form = document.querySelector('form');
// The most common pattern: intercept submit, handle with JS
form.addEventListener('submit', (e) => {
e.preventDefault(); // stop the browser from navigating
const data = new FormData(form);
const username = data.get('username');
const password = data.get('password');
fetch('/api/login', {
method: 'POST',
body: JSON.stringify({ username, password }),
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(result => {
if (result.ok) {
window.location.href = '/dashboard';
} else {
alert('Login failed');
form.reset();
}
});
});Related Elements
Related Properties
Related Methods