#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

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: .value gives the selected option's value attribute, or its text if there is no value attribute. For a multiple select, loop over .selectedOptions.
  • Checkbox: .checked is true or false. Its .value is just the fixed value attribute, which defaults to "on", so it does not tell you whether it is ticked.
  • Radio group: form.elements.plan returns a RadioNodeList, and its .value is 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.

Explore Course