Accessing data from forms
Accessing form data means reading what the user typed, selected or ticked in a form's fields using JavaScript, usually when the form is submitted. Browsers give you several ways to do it: through the value of individual inputs, through the form's elements collection, or all at once with the FormData object. You need this whenever a form's data has to be validated, displayed or sent to a server with JavaScript.
Reaching fields by name
Every form on the page is available in document.forms, and each form has an elements collection where fields can be looked up by their name attribute. This is often tidier than giving each input an id.
<form name="signup" id="signup">
<input name="fullName">
<input name="age" type="number">
<select name="city">
<option>Delhi</option>
<option>Pune</option>
</select>
<label><input type="radio" name="plan" value="basic" checked> Basic</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
<label><input type="checkbox" name="news"> Send me updates</label>
<button>Sign up</button>
</form>
const form = document.forms.signup;
form.addEventListener('submit', (event) => {
event.preventDefault(); // stop the page from reloading
const { fullName, age, city, plan, news } = form.elements;
const data = {
fullName: fullName.value.trim(),
age: Number(age.value), // value is always a string
city: city.value, // selected option's value
plan: plan.value, // checked radio in the group
wantsNews: news.checked, // checkboxes use .checked
};
console.log(data);
});
Each field type has its own property
- Text, email, number, textarea: read
.value, which is always a string. - Select:
.valuegives the selected option'svalueattribute, or its text if there is no value attribute. For amultipleselect, loop over.selectedOptions. - Checkbox:
.checkedistrueorfalse. Its.valueis just the fixed value attribute, which defaults to"on", so it does not tell you whether it is ticked. - Radio group:
form.elements.planreturns aRadioNodeList, and its.valueis the value of the checked radio, or an empty string if none is checked.
Collecting everything with FormData
new FormData(form) gathers every named, enabled field in one step, following the same rules the browser uses when it submits the form normally. Unchecked checkboxes are left out entirely.
To turn it into a plain object, use Object.fromEntries(new FormData(form)). For fields that can have several values, such as multiple checkboxes with the same name, use formData.getAll('name') instead, because fromEntries keeps only the last one. You can also pass a FormData object directly as the body of a fetch request, and the browser sets the right content type, which is how file uploads are usually sent.
Two common slip-ups
The first is reading values too early. Code like const name = input.value; at the top of a script runs when the page loads, before the user types anything, so it captures an empty string. Read values inside the submit or input event handler, at the moment you need them.
The second is forgetting that values are strings. Adding two number fields with a.value + b.value joins them, so "2" and "3" become "23". Convert with Number() or use input.valueAsNumber, and check for NaN when the field is empty. Also remember that fields without a name attribute are ignored by FormData.
What to remember
Listen for the form's submit event, call preventDefault(), and read fields through form.elements or FormData. Use .value for most fields, .checked for checkboxes, and convert numbers yourself. The next lesson uses these values to validate input before submitting.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.