An entry for the show
<form>, value and checked, the submit event, preventDefault, FormData and a button you can't press twice.
A form is a <form> tag with fields inside:
<input>— a line of text, a number, an email, a password, a checkbox (type="checkbox");<select>— a drop-down list;<button>— a button. Inside a form it submits the form by default.
Every field has a name attribute — that's the name its value is sent to the server under. From JavaScript, you read a field through its value property.
const age = document.querySelector("#age");
console.log(age.value, typeof age.value);
console.log(age.value + 1);
console.log(Number(age.value) + 1);type="number" field holds a string. + glues it instead of adding.const vaccinated = document.querySelector("#vaccinated");
const breed = document.querySelector("#breed");
console.log(vaccinated.value, vaccinated.checked);
vaccinated.checked = true;
console.log(vaccinated.checked);
console.log(breed.value);checked (true/false); for a list, the value of the chosen option.When the user clicks the form's button or presses Enter in a field, the form fires a submit event. That's the one to listen for, not a click on the button: this way you catch Enter and the "Go" key on a phone keyboard too.
But submit has a default action: the browser sends the form itself and reloads the page. Everything your JavaScript did is gone. To take over the submission, call event.preventDefault().
const form = document.querySelector("#entry");
const name = document.querySelector("#name");
form.addEventListener("submit", (event) => {
event.preventDefault();
console.log("Entry from:", name.value.trim());
});
name.value = " Whiskers ";
document.querySelector("button").click(); // as if someone clicked the buttonpreventDefault() cancels the reload, and trim() removes stray spaces.Reading every field one by one is tedious. new FormData(form) collects all the form's fields that have a name — exactly what the browser would send to the server. data.get("name") returns one value, data.getAll("tags") returns every value with that name, and Object.fromEntries(data) turns the lot into a plain object.
const form = document.querySelector("#entry");
const data = new FormData(form);
console.log(data.get("name"), data.get("age"));
console.log(data.get("vip"));
console.log(JSON.stringify(Object.fromEntries(data)));FormData: get gives null.A good form helps people fix their mistakes:
- the message sits next to the field and says what to do: "Enter the age", not "Validation error";
- spaces at the edges are trimmed (trim) before checking;
- while the entry is being sent, the button is disabled (
button.disabled = true) — otherwise an impatient owner clicks five times and sends five identical cats.
The browser can check simple rules by itself: the required attribute (the field must be filled) and pattern (a regex template). The trickier ones — "the passwords match", "age is from 0 to 30" — are JavaScript's job.
const button = document.querySelector("button");
const send = () => new Promise((resolve) => setTimeout(resolve, 300));
button.disabled = true;
button.textContent = "Sending…";
console.log("Button disabled:", button.disabled);
await send();
button.disabled = false;
button.textContent = "Register";
console.log("Button disabled:", button.disabled);value is a string, checked is for checkboxes, submit lives on the form, preventDefault keeps the page from reloading, and FormData collects everything at once. I've also figured out why a form reloads the page at all. That story is older than JavaScript — next lesson.Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.