×

javascript · level 2 · unit 4 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

After a good answer, the form and the first field.

Once an answer is used, the form should be ready for the next one. form.reset() puts every field back the way the page started: empty boxes, unticked checkboxes, first dropdown choices.

The field with focus is the one your typing goes into. Call snack.focus() and the cursor jumps into that field, so the next answer can be typed straight away, with no clicking.

To empty one field only, set its .value to an empty string, "".

<form id="add">
  <input id="snack" placeholder="Add a snack">
  <button>Add</button>
</form>
<ul id="snacks"></ul>
const form = document.querySelector("#add");
const snack = document.querySelector("#snack");
form.addEventListener("submit", (event) => {
  event.preventDefault();
  const item = document.createElement("li");
  item.textContent = snack.value;
  document.querySelector("#snacks").append(item);
  form.reset();
  snack.focus();
});

Press run it to bring the page to life.

console

nothing logged yet

Read every answer first. After reset(), the old answers are gone.