Forms object
The forms object refers to the way the browser exposes every <form> on a page to JavaScript, through the document.forms collection and through each form's own properties and methods. You use it to find a form, reach its fields by name, read or change their values, and submit or reset it from code.
Finding forms with document.forms
document.forms is a live, array-like collection of all forms in the document, in source order. You can access a form by index, but it is far more reliable to give it a name or id and access it by that, since adding another form higher up the page would shift every index.
<form name="enquiry" id="enquiry">
<input name="fullName" required>
<input name="email" type="email">
<select name="city">
<option>Delhi</option>
<option>Noida</option>
</select>
<label><input type="checkbox" name="updates"> Send me updates</label>
<button>Send</button>
</form>
<script>
const form = document.forms.enquiry; // or document.forms['enquiry']
console.log(document.forms.length); // number of forms on the page
console.log(form.elements.length); // 5 controls, including the button
const { fullName, city, updates } = form.elements;
fullName.value = 'Asha';
city.value = 'Noida';
updates.checked = true;
</script>
The elements collection
Each form has an elements property listing its controls: inputs, selects, textareas, buttons and fieldsets. Like document.forms, it can be indexed by number or by the control's name. That makes code like form.elements.email.value short and readable, and it keeps working even if you restyle or rearrange the markup.
When several controls share one name, as radio buttons do, form.elements.gender returns a RadioNodeList. Its value property gives the value of whichever radio is currently checked, which saves you from looping over them. Setting that same value checks the matching radio for you.
Controls placed outside the form tag can still belong to it through the form="enquiry" attribute, and they then appear in elements too.
Useful form properties and methods
| Member | What it does |
|---|---|
form.action, form.method | Read or set where and how the form submits. |
form.reset() | Restores every field to its original value. |
form.requestSubmit() | Submits as if the user clicked submit, running validation and firing the submit event. |
form.submit() | Submits immediately, skipping validation and the submit event. |
form.checkValidity() | Returns true if every field passes its HTML constraints. |
input.form | From any control, points back to its parent form. |
The submit() surprise
A frequent source of confusion is calling form.submit() and finding that your submit event listener never runs and the required checks are ignored. That is by design: submit() bypasses both. If you want submitting from code to behave exactly like a user click, use requestSubmit() instead.
Another trap: never name a field submit, action or reset. Named controls become properties of the form, so <input name="submit"> hides the real form.submit method and calling it throws "form.submit is not a function".
Reading all values at once
For forms with many fields, new FormData(form) collects every named, enabled control in one step. Object.fromEntries(new FormData(form)) turns it into a plain object, which is handy for sending JSON. Unchecked checkboxes are left out entirely, so check for their absence rather than for false. Reading data in detail is the focus of the next lesson.
Remember
- Give forms and fields
nameattributes and access them by name, not position. form.elementsis the clean way to reach each control.- Use
requestSubmit()when you want validation and submit handlers to run.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.