CyberStars

Reading a Box

In a café, you tell the person at the counter your name, and they write it on your cup. They read what you said, and write it somewhere else. A script can do the same with a form: read what the visitor typed in a box, and write it on the page.

.value: what is in a box now

Every box has a value: the text that is in it right now. A script reads it with .value:

<label for="nickname">Nickname</label>
<input id="nickname" />
<button type="button" id="hello">Say hello</button>
<p id="answer"></p>
<script>
  const nickname = document.querySelector("#nickname");
  const answer = document.querySelector("#answer");

  document.querySelector("#hello").addEventListener("click", () => {
    answer.textContent = `Hello, ${nickname.value}!`;
  });
</script>

Type Rocket in the box and click Say hello. The paragraph says:

Hello, Rocket!

Now type Comet instead, and click again. The paragraph says Hello, Comet!. Each click reads the box again, so it always gets what is in it at that moment.

A box doesn't have text between two tags, like a paragraph does. So textContent doesn't help here. What the visitor types is kept in the box's value.

When you read it matters

Here, the script reads the box when the page opens, and keeps the text in a variable:

<label for="nickname">Nickname</label>
<input id="nickname" />
<button type="button" id="hello">Say hello</button>
<p id="answer"></p>
<script>
  const typed = document.querySelector("#nickname").value;
  const answer = document.querySelector("#answer");

  document.querySelector("#hello").addEventListener("click", () => {
    answer.textContent = `Hello, ${typed}!`;
  });
</script>

Type Rocket and click:

Hello, !

When the page opened, the box was empty, so typed holds an empty text. Typing later doesn't change the variable. Read .value inside the click's function, after the visitor has typed.

Break it on purpose

Use textContent on the box:

answer.textContent = `Hello, ${nickname.textContent}!`;

Type Rocket and click: the paragraph says Hello, ! again. There is no error. The box has no text between tags, so its textContent is always empty. For a box, use .value.


Mission: The boarding pass

What you will see: under the form, a small boarding pass with a heading, a line for the passenger and a button. Type Ida Moreau in the name box and click Show my pass. The pass now says something like:

Your boarding pass
Passenger: Ida Moreau

Your booking form is ready, but boarding.html has no script yet. The editor has a new, empty file for it: boarding.js.

  • boarding.html loads boarding.js from its head, with defer, like your other pages load theirs.
  • Under the form, add a button with the id show-pass, which says Show my pass.
  • A click on that button never sends the form.
  • A click on it writes what is in the name box in an element with the id pass-name.
  • That element holds only the name: words like Passenger: go outside it.
  • Type another name and click again: the pass shows the new name.