Introduction to forms
HTML forms collect input, and JavaScript lets you work with that input in the browser before, or instead of, sending it to a server. With a few DOM properties you can find a form, read and set field values, listen for submission, and respond without reloading the page. This is the foundation for search boxes, login screens, calculators and checkout pages.
Finding a form and its fields
You can select a form like any element, with document.querySelector('#signup'). Every form also appears in the document.forms collection, accessible by its name or id. Once you have the form, its elements property lists every control inside it, and you can reach each one by its name attribute. That means you rarely need a separate id on every input.
<form id="signup" name="signup">
<input name="fullName" required>
<input name="email" type="email" required>
<select name="plan">
<option value="basic">Basic</option>
<option value="pro">Pro</option>
</select>
<label><input name="terms" type="checkbox"> I agree</label>
<button>Create account</button>
</form>
<p id="result"></p>
<script>
const form = document.forms.signup;
form.addEventListener('submit', event => {
event.preventDefault();
const { fullName, email, plan, terms } = form.elements;
if (!terms.checked) {
document.querySelector('#result').textContent = 'Please accept the terms.';
return;
}
document.querySelector('#result').textContent =
`Welcome ${fullName.value.trim()}, you chose ${plan.value}.`;
});
</script>
Reading different kinds of fields
- Text, email, number and textarea fields expose
valueas a string, even fortype="number". Convert withNumber()or usevalueAsNumber. - A
selectgives thevalueof the chosen option;selectedIndexgives its position. - Checkboxes and radio buttons use
checked, a boolean. A group of radios sharing a name appears as a list whosevalueis the selected one. - File inputs expose a
fileslist rather than a usable value.
Submit and other form events
The submit event fires on the form when the user clicks a submit button or presses Enter in a field. Listening there, rather than on the button's click, covers both. input fires on every change to a field, which suits live character counters. change fires when the user commits a change, such as leaving a text field or picking a new option. The reset event fires when the form is cleared with form.reset() or a reset button.
For collecting everything at once, new FormData(form) gathers all named fields, and Object.fromEntries(new FormData(form)) turns them into a plain object.
Setting values works the same way in reverse. Assigning form.elements.plan.value = 'pro' selects that option, and terms.checked = true ticks the box. This is useful for pre-filling a form from saved data. Calling form.reset() restores every field to the value written in the HTML, not to empty, so fields with a value attribute return to that original text.
Where beginners trip
Forgetting event.preventDefault() makes the page reload the moment the form submits, so your message flashes and vanishes. If that happens, check that the listener is on the submit event and calls it first.
Another trap is naming a field submit or action. Because fields are exposed as properties of the form, a field named submit hides the form's submit() method. Choose distinctive names.
Lastly, fields without a name attribute are left out of FormData and of normal submissions.
Remember
Reach fields through form.elements by name, use value for text and checked for boxes, and handle the submit event with preventDefault().
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.