Register to get access to free programming courses with interactive exercises


There are two approaches to working with forms in modern front-end applications.

Sometimes, data on the page changes immediately as the form changes. These forms don't have any Save or Send buttons. Each form element is associated with a handler that tracks any changes. A typical example could be filtering data on a page.

In a more classic version, we send the form with a button. In this case, you need to use the submit form event:

const form = document.querySelector(/* form selector */);
form.addEventListener('submit', (e) => {
   // If we want to work with a form via JavaScript, then we need to stop it from sending(default action)
  // Doing something

Why not put a click event on the form submit button? Technically, we can do it, but it'll break the standard behavior. Browsers allow you to send forms from the keyboard by pressing the Enter key. Here, we send forms without pressing a button.

Processing the form is usually set up like this:

  • The data from the form are extracted
  • A request is made to the server / Data in the application are changed
  • The appearance changes

We'll look at the second and third points in future lessons when we discuss AJAX. Here we'll talk about the first. How to get the form data.

There are two approaches: the right one and the wrong one. Let's look at the wrong one:

const input = document.querySelector(/* input field selector */);
form.addEventListener('submit', (e) => {
  const { value } = input;
  // Doing something with the data

With this approach, you need to work with each form element individually. First, extract it from the DOM, then collect the values. There's no need to do this, however; you can do it correctly instead.

The correct way is to use a FormData object, which is available in the browser:

<form method="post">
  <input name="email" value="">
  <input name="password" value="supersecret">
  <input type="submit" value="Sign Up">
const form = document.querySelector(/* the required form's selector */);
form.addEventListener('submit', (e) => {
  // We extract the form data from DOM automatically
  // Then we pass to the input a form element taken from the event
  const formData = new FormData(;
  // Now you can work with them
  formData.get('email'); //
  // values() returns an iterator, so we convert it to an array
  [...formData.values()]; // ['', 'supersecret']
  // Also an iterator
  // [['email', ''], ['password', 'supersecret']]

  // Conversion to a regular object
  // { email: '', password: 'supersecret' }

Extracting form elements

Sometimes, however, you have to access the form elements directly when we implement validation while changing the form, not when we send it.

In such cases, work with the elements of the form goes directly:

const input = document.querySelector(/* selector before the input field */);
input.addEventListener('change', (e) => {
  // Logic

If there are a lot of elements, then the code executing queries in the DOM will get cumbersome. We can avoid it by using the DOM features related to forms.

Each form contains the elements property, which returns an object with all elements of that form. The object keys are the names of the elements, and the values are the elements themselves:

<form method="post">
  <input name="email" value="">
  <input name="password" value="supersecret">
  <input type="submit" value="Sign Up">
const form = document.querySelector(/* selector to form */); // <input name="email" ...
form.elements.password // <input name="password" ...

// Processing'change', () => {
  // Processing

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.