Read what someone types with .value, react as they type, and turn text into a number with Number().
A calculator on a web page reads what you typed, then answers. You'll build one.
An input box has a value: whatever is typed in it right now. Read it inside a handler:
out.textContent = "Hello, " + nameBox.value + "!";
Two events are useful: "click" on a button, and "input" on the box itself, which fires on every keystroke.
The hard part is that .value is always text, even when someone types a number. "4" + "5" is "45", not 9. Turn it into a number first: Number(a.value).
Make your prediction first. Help opens once you've locked it in.
<input id="name" placeholder="Your name">
<button id="greet">Greet</button>
<p id="out"></p>
<script>
const nameBox = document.querySelector("#name");
const out = document.querySelector("#out");
document.querySelector("#greet").addEventListener("click", function () {
out.textContent = "Hello, " + nameBox.value + "!";
});
</script>
When is nameBox.value read: when the page loads, or when the button is clicked?
Press Run, then type in the box. The count changes on every keystroke, because the listener is on the "input" event.
<textarea id="msg" rows="3" cols="30" placeholder="Type here"></textarea>
<p><span id="count">0</span> characters</p>
<script>
const msg = document.querySelector("#msg");
const count = document.querySelector("#count");
msg.addEventListener("input", function () {
count.textContent = msg.value.length;
});
</script>
<textarea id="msg" rows="3" cols="30" placeholder="Type here"></textarea>
<p><span id="count">0</span> characters</p>
<script>
const msg = document.querySelector("#msg");
const count = document.querySelector("#count");
msg.addEventListener("input", function () {
count.textContent = msg.value.length;
});
</script>
Match code with meaning.
What + does to text.
The sum is wrong, and nothing complains.
Match each piece with what it does.
The page shows 45 instead of 9. There is no red error. Click the line that is wrong.
Numbers, not text.
Before a hint: what kind of stuck is this? Try a strategy first
The page is adding text. What does + do with text?
Wrap each value in Number(...): Number(a.value) + Number(b.value).

Decide as they type.
Before a hint: what kind of stuck is this? Try a strategy first
An if inside the handler can look at how many characters there are.
Add a paragraph for the warning, and if (msg.value.length > 20) { … } to fill it.

Make the Add button show 9 for 4 + 5.
This ticks when your page has all of the above.
Show Too long! in red when the message has more than 20 characters.
This ticks when your page has all of the above.
Make a small tool that reads a number from a box and shows an answer: a tip calculator, a temperature converter (°C to °F), an age-in-days counter, a unit converter. Check for a bad number with isNaN(...), as the page on this lesson does.
This page splits a restaurant bill between friends. It reads two boxes, checks that they hold sensible numbers and shows each person's share. Add a third box for a tip in percent, and make each share include it. For a $60 bill with a 10% tip and 4 people, each person should pay $16.50.
Before a hint: what kind of stuck is this? Try a strategy first
Add a third box with its own id, read it with Number(...) like the other two, and work the tip out before you divide.
Add a tip box, and include the tip in what each person pays.
This ticks when your page has all of the above.
You can now read what people type. Next: build a list on the page.
Lesson done. You've finished every step you need.
Students read an input with .value, react as they type with the input event, find that values are text and use Number() to add, and make a calculator or converter of their own. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. This is the Python input() lesson with a twist: there is no program waiting, so the page reacts to an event. input fires on every keystroke; click on a button reads the box once. The big idea is that .value is always text, as input() was in Python: the same "45" surprise. The bug has no red error, so students must read the answer. Number("abc") is NaN; isNaN(x) checks for it, used in the Make example. The playground cannot send a form, on purpose: use a button and an event, not a <form>.
Text versus number: "4" + "5" is "45". Show the page, ask what + does to text, and only then show Number().
Stretch: Make the result appear live, with no button at all, using the input event on each box.
Support: Give the greeting page and ask students to change only the words, then add a second box.
Next lesson: lists of elements: making new elements and adding them to the page.