×

javascript · level 2 · unit 2 · lesson 3

  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

A 's .value holds whatever someone has typed.

An <input> tag makes a field, the box you type a name or a search into. Its .value property holds what is in it right now, always as a string, even when someone types a number.

A field fires an input event each time its words change. Listen for that and your code can react to every key: a live preview, a letter counter, a greeting that types along with you.

Need a number? Wrap the value in Number(), as you did with prompt.

<input id="nickname" placeholder="Your name">
<p id="preview">Hello, ...</p>
const field = document.querySelector("#nickname");
const preview = document.querySelector("#preview");
field.addEventListener("input", () => {
  preview.textContent = `Hello, ${field.value}!`;
});

Press run it to bring the page to life.

console

nothing logged yet

.value is a property, not a method, so there are no brackets after it.