AI Hints
Home›JavaScript Tutorial›Events and Forms
JavaScript · Lesson 98 of 138

Forms and Input

Reading what people type.

⏱ 2 min read▶ Video at 1:57:20🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Forms are how people give a page information. Names, emails, search terms. JavaScript can read what they type, check it, and respond instantly, without sending anything to a server.

Forms and Input
From the video: Forms and Input
  • Names, emails, searches
  • Read what they type
  • Respond instantly

A greeting form

An input's value is the text inside it. When the form is submitted, we call prevent default, so the page doesn't reload. Then we trim the name, and check it isn't empty. And we show a greeting. Here, we typed Ana and pressed enter.

index.htmlpython
<link rel="stylesheet" href="style.css">
<form id="form">
  <input id="name" placeholder="Your name">
  <button>Say hi</button>
</form>
<p id="out" class="big"></p>
<script>
  const out = document.querySelector("#out");
  document.querySelector("#form").addEventListener("submit", (e) => {
    e.preventDefault();
    const name = document.querySelector("#name").value.trim();
    out.textContent = name ? `Hello, ${name}!` : "Please type a name";
  });
</script>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"type": "#name", "text": "  Ana "}, {"key": "Enter"}]

Live input

The input event fires on every key stroke. Here, as you type, the page counts the characters, and turns the count red when you go over twenty. We typed a long sentence, so the counter shows the total in red.

index.htmlpython
<link rel="stylesheet" href="style.css">
<input id="msg" placeholder="Write a short message">
<p id="count" class="big">0 / 20</p>
<script>
  const msg = document.querySelector("#msg");
  const count = document.querySelector("#count");
  msg.addEventListener("input", () => {
    const n = msg.value.length;
    count.textContent = `${n} / 20`;
    count.style.color = n > 20 ? "#ff6b6b" : "";
  });
</script>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} .big{font-size:64px;font-weight:800}
Also uses actions.json
[{"type": "#msg", "text": "This message is far too long"}]

Summary

Read an input's value, use the submit event with prevent default for forms, and the input event to react as people type. Next, practice with a counter app.

Key takeaways
  • input.value
  • submit + preventDefault
  • input event
Next up: Practice: A Counter App — buttons and state