#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

Form Handling

Form handling in JavaScript means finding a form and its fields in the DOM, reacting when the user types or submits, and reading the values so you can check or send them. It builds directly on selecting nodes: once you can grab elements, you can listen to their events and read their properties.

Reaching forms and fields

You can select a form with document.querySelector or getElementById like any element. The DOM also offers special collections: document.forms lists every form on the page, and each form has an elements collection holding its controls. Both can be indexed by the name attribute, which keeps code readable.

<form name="signup">
  <input name="username" required>
  <input name="age" type="number">
  <label><input name="newsletter" type="checkbox"> Send me updates</label>
  <p id="status"></p>
  <button type="submit">Join</button>
</form>

<script>
  const form = document.forms.signup;
  const { username, age, newsletter } = form.elements;
  const status = document.getElementById("status");

  username.addEventListener("input", () => {
    status.textContent = `${username.value.length} characters`;
  });

  form.addEventListener("submit", (event) => {
    event.preventDefault();
    const details = {
      username: username.value.trim(),
      age: age.valueAsNumber,
      newsletter: newsletter.checked,
    };
    status.textContent = `Welcome, ${details.username}!`;
    form.reset();
  });
</script>

The events that matter

EventFires whenTypical use
inputEvery change to the value, as it happensLive character counts, instant search
changeThe value is committed (on blur for text, immediately for checkboxes and selects)Reacting to a final choice
focus / blurThe field gains or loses focusShowing hints, validating on leave
submitThe form is submitted by button or Enter keyCollecting and sending data

Reading values from different controls

Text inputs, textareas and selects expose their content through .value, which is always a string. Number and date inputs also offer valueAsNumber and valueAsDate. Checkboxes and radio buttons use .checked, a boolean. For a radio group, form.elements.groupName.value gives the value of the selected option. A multi-select needs selectedOptions to read all chosen items.

Stopping the page reload

By default, submitting a form makes the browser navigate to the form's action URL, or reload the current page if there is none. That wipes out anything your script was doing. Calling event.preventDefault() inside the submit listener cancels that navigation so you can handle the data with JavaScript, for example by sending it with fetch.

Listening to the button instead of the form

A common beginner mistake is attaching a click listener to the submit button. It misses submissions made by pressing Enter in a field, and it fires even when the browser's built-in validation would have blocked the form. Listen to the form's submit event instead; it fires only after built-in checks such as required have passed.

Another slip is a button inside a form without a type. Buttons default to type="submit", so a "Show password" or "Add another" button will unexpectedly submit the form. Give such buttons type="button".

Giving feedback while the user works

Good form handling talks back. Show a short message next to a field when its value is wrong, disable the submit button while a request is in progress so it cannot be sent twice, and put the cursor back in the first problem field with field.focus(). Small touches like these make a form feel reliable, and they all use the same handful of DOM properties and events covered above.

Takeaway: access fields through form.elements, use input for live feedback and submit for sending, read checked for checkboxes, and always call preventDefault when JavaScript handles the submission.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course