Form Handling
Form handling in JavaScript means finding a form and its fields in the DOM, reacting when the user types or submits, and reading the values so you can check or send them. It builds directly on selecting nodes: once you can grab elements, you can listen to their events and read their properties.
Reaching forms and fields
You can select a form with document.querySelector or getElementById like any element. The DOM also offers special collections: document.forms lists every form on the page, and each form has an elements collection holding its controls. Both can be indexed by the name attribute, which keeps code readable.
<form name="signup">
<input name="username" required>
<input name="age" type="number">
<label><input name="newsletter" type="checkbox"> Send me updates</label>
<p id="status"></p>
<button type="submit">Join</button>
</form>
<script>
const form = document.forms.signup;
const { username, age, newsletter } = form.elements;
const status = document.getElementById("status");
username.addEventListener("input", () => {
status.textContent = `${username.value.length} characters`;
});
form.addEventListener("submit", (event) => {
event.preventDefault();
const details = {
username: username.value.trim(),
age: age.valueAsNumber,
newsletter: newsletter.checked,
};
status.textContent = `Welcome, ${details.username}!`;
form.reset();
});
</script>
The events that matter
| Event | Fires when | Typical use |
|---|---|---|
input | Every change to the value, as it happens | Live character counts, instant search |
change | The value is committed (on blur for text, immediately for checkboxes and selects) | Reacting to a final choice |
focus / blur | The field gains or loses focus | Showing hints, validating on leave |
submit | The form is submitted by button or Enter key | Collecting and sending data |
Reading values from different controls
Text inputs, textareas and selects expose their content through .value, which is always a string. Number and date inputs also offer valueAsNumber and valueAsDate. Checkboxes and radio buttons use .checked, a boolean. For a radio group, form.elements.groupName.value gives the value of the selected option. A multi-select needs selectedOptions to read all chosen items.
Stopping the page reload
By default, submitting a form makes the browser navigate to the form's action URL, or reload the current page if there is none. That wipes out anything your script was doing. Calling event.preventDefault() inside the submit listener cancels that navigation so you can handle the data with JavaScript, for example by sending it with fetch.
Listening to the button instead of the form
A common beginner mistake is attaching a click listener to the submit button. It misses submissions made by pressing Enter in a field, and it fires even when the browser's built-in validation would have blocked the form. Listen to the form's submit event instead; it fires only after built-in checks such as required have passed.
Another slip is a button inside a form without a type. Buttons default to type="submit", so a "Show password" or "Add another" button will unexpectedly submit the form. Give such buttons type="button".
Giving feedback while the user works
Good form handling talks back. Show a short message next to a field when its value is wrong, disable the submit button while a request is in progress so it cannot be sent twice, and put the cursor back in the first problem field with field.focus(). Small touches like these make a form feel reliable, and they all use the same handful of DOM properties and events covered above.
Takeaway: access fields through form.elements, use input for live feedback and submit for sending, read checked for checkboxes, and always call preventDefault when JavaScript handles the submission.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.