input: the page hears you as you type
Section 7: Events and formsLesson 7.1 · ⏱ 8 min
addEventListener("input", …) runs the function with every letter the visitor types, so the page changes as they type.
HTML
<h2>Name card</h2>
<input id="name" placeholder="Your name" maxlength="20">
<p id="hello">Hello!</p>
<small id="count">0 / 20</small>
CSS
body { font-family: Tahoma, sans-serif; }
input { font-size: 18px; padding: 6px; }
#hello { font-size: 28px; color: #2557c9; }
JavaScript
const box = document.getElementById("name");
box.addEventListener("input", function () {
const name = box.value;
document.getElementById("hello").textContent = "Hello " + name + " 👋";
document.getElementById("count").textContent = name.length + " / 20";
});
🖥️ The page in Robo Web:

🖥️ What happens on the stage?
We typed “Youssef”, the greeting changed, and the counter became 7 / 20.
- Make the greeting red if the name is longer than 10 letters.
📌 Another example: the temperature converter
We type the temperature in Celsius, and it appears in Fahrenheit as we type.
HTML
<input id="c" type="number" placeholder="°C">
<p id="f">? °F</p>
CSS
body { font-family: Tahoma, sans-serif; }
input { font-size: 20px; width: 100px; }
#f { font-size: 28px; }
JavaScript
const c = document.getElementById("c");
c.addEventListener("input", function () {
const f = Number(c.value) * 9 / 5 + 32;
document.getElementById("f").textContent = f + " °F";
});
🖥️ The page in Robo Web:

✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
What you type appears under it as it is.
👀 Show the solution🙈 Hide the solution
HTML
<input id="t">
<p id="copy"></p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
const t = document.getElementById("t");
t.addEventListener("input", function () {
document.getElementById("copy").textContent = t.value;
});
🖥️ The page in Robo Web:

Result: Hi
Medium
The slider (range) changes the emoji's size.
👀 Show the solution🙈 Hide the solution
HTML
<input id="size" type="range" min="20" max="120" value="40">
<div id="e">🐳</div>
CSS
body { font-family: Tahoma, sans-serif; }
#e { font-size: 40px; }
JavaScript
const s = document.getElementById("size");
s.addEventListener("input", function () {
document.getElementById("e").style.fontSize = s.value + "px";
});
🖥️ The page in Robo Web:

Result: the whale grew to 100px.
Hard
The password: “Strong ✅” appears if it is 8 letters or more, otherwise “Weak ❌”.
👀 Show the solution🙈 Hide the solution
HTML
<input id="pw" type="password">
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
const pw = document.getElementById("pw");
pw.addEventListener("input", function () {
if (pw.value.length >= 8) document.getElementById("msg").textContent = "Strong ✅";
else document.getElementById("msg").textContent = "Weak ❌";
});
🖥️ The page in Robo Web:

Result: weak, then strong.

