Lesson 6 of 8

Read the whole form at once

Goal

Read every field in one line

01

Explanation

If you read a form field by field, the code grows with every new field, and you might forget the one that was added yesterday. new FormData(form) reads every field at once, and Object.fromEntries turns the result into a plain object, ready for JSON.stringify and for sending.

But FormData only sees fields that have a name. A field without one is dropped silently: no error, no warning, just a key missing from the request. And every value arrives as a string, so the quantity comes in as "2", not 2, and "2" + 1 gives "21".

A single input listener on the form covers all of its fields, because the event bubbles up to it from any field inside.

02

Your task

Write readForm(form) so it returns an object with every field and the quantity as a number, fix the field that gets dropped, then show JSON.stringify(readForm(form)) in #out on every edit.

Checks

5 checks
  1. readForm reads the current values
  2. The email is in the output
  3. The quantity is a number, not a string
  4. A new field is read without changing the code
  5. The displayed output updates on every edit
03

Hints

0/3
Track lessons0 of 8 done

In this track you'll write your own validation rules, tie one field to another, and write error messages a screen reader can announce.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

In this track you'll write your own validation rules, tie one field to another, and write error messages a screen reader can announce.

Intermediate level, 8 lessons, 1.5 hours in total.