Sam would rather press Enter than reach for the mouse, but on the last lesson’s page, Enter in the How many? box did nothing. In this video, you wrapped the shopping list’s fields in a form, watched the page reload, and moved the Add code to a listener that keeps the page in place. Here are the key points:
- A
<form>element groups fields that are sent together. Pressing Enter in one of its text boxes sends the form, when the form has a submit button, as yours does. - Sending a form is built into the browser. With this form’s default settings, the browser loads a page and puts the named field values after a question mark. A form that doesn’t say which page to load loads its own page again, so the boxes empty and your script starts over. The shopping list’s fields have no
nameattributes yet, so nothing follows the?. Anameisn’t the same as anid. A reload keeps that?, so load the plain address before each try. - An ordinary button in this form defaults to submit. The Add button has no
type, so clicking it sends the form. For a button that runs your own code, such as a Clear list button, usetype="button". - Pressing Enter in this form’s boxes clicks its submit button for you. With
event.preventDefault()in the Add button’s click listener, Enter still added the row, because the click listener ran and canceled the send. - A click listener that calls
preventDefault()cancels the send before the browser checks the form, for example that a required box isn’t empty, so those checks never run. You’ll set up checks later in this course. That’s all you need about them for now. - The form fires a
submitevent when it’s really being sent, only after its checks pass. So listen forsubmiton the form, notclickon its button. - Loading a page is what sending does by default. Before,
preventDefault()stopped the click from sending the form, sosubmitnever fired. A submit listener withoutpreventDefault()runs, and then the browser reloads the page anyway. Makeevent.preventDefault()the submit listener’s first line, and the page stays put.
In kitchen/index.html, the two labels and the Add button now sit inside a form with the id add-form, and the rest of <main> stays as it was:
kitchen/index.html
<form id="add-form">
<label>
Item
<input id="item-name">
</label>
<label>
How many?
<input id="item-count" type="number">
</label>
<button id="add-item">Add</button>
</form>
In kitchen/main.js, store the form in a constant named form beside the other lookups, and change the Add listener’s first line to listen for submit on the form, with event.preventDefault(); first. The checks and addItem() are the last lesson’s, unchanged. Here’s the whole file:
kitchen/main.js
const nameInput = document.querySelector("#item-name");
const countInput = document.querySelector("#item-count");
const addButton = document.querySelector("#add-item");
const nextLine = document.querySelector("#next");
const message = document.querySelector("#message");
const itemList = document.querySelector("#items");
const form = document.querySelector("#add-form");
function addItem(name, count) {
const item = document.createElement("li");
item.textContent = `${name} (${count})`;
itemList.append(item);
}
form.addEventListener("submit", (event) => {
event.preventDefault();
const name = nameInput.value.trim();
const countText = countInput.value.trim();
const count = Number(countText);
if (name === "") {
message.textContent = "Type the item's name first.";
} else if (countText === "" || Number.isNaN(count)) {
message.textContent = "Type how many you need, like 3.";
} else {
message.textContent = "";
addItem(name, count);
}
});
function showNext() {
const name = nameInput.value.trim();
nextLine.textContent = `Next: ${name} (${countInput.value})`;
}
nameInput.addEventListener("input", showNext);
countInput.addEventListener("input", showNext);
Load the page at its plain address, type eggs and 3, and press Enter. The row eggs (3) appears, and the address stays the same. The boxes keep what you typed, so clicking Add puts a second eggs (3) row on the list. Emptying the form comes in the next lesson.
Now Enter and the Add button both add Sam’s item, and nothing reloads. Next, in Read a Whole Form With FormData, you’ll read a bigger form all at once.