#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

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 value as a string, even for type="number". Convert with Number() or use valueAsNumber.
  • A select gives the value of the chosen option; selectedIndex gives its position.
  • Checkboxes and radio buttons use checked, a boolean. A group of radios sharing a name appears as a list whose value is the selected one.
  • File inputs expose a files list 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.

Explore Course