Chapter 5 · The web page

Events

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A page that answers

By the end of this lesson your pages answer what the user does. You will make:

  • a button that counts its clicks,
  • a form that reads what is typed,
  • a list that redraws itself from an array.
const button = document.querySelector("#hello");
button.addEventListener("click", () => {
  console.log("clicked");
});
console.log("ready");
let count = 0;
const plus = document.querySelector("#plus");
const minus = document.querySelector("#minus");
const output = document.querySelector("#count");
// 1. a click on plus adds 1 to count
// 2. a click on minus takes 1 away
// 3. both show Count: and the count in output
for (const button of document.querySelectorAll("button")) {
  button.addEventListener("click", (event) => {
    console.log(event.target.textContent);
  });
}
const input = document.querySelector("#amount");
console.log(input.value + 1);
const input = document.querySelector("#amount");
const result = document.querySelector("#result");
input.addEventListener("input", () => {
  result.textContent = input.value + 10;
});
const form = document.querySelector("#greet");
const name = document.querySelector("#name");
const hello = document.querySelector("#hello");
form.addEventListener("submit", (event) => {
  event.preventDefault();
  hello.textContent = `Hello, ${name.value}`;
});
const items = [];
const form = document.querySelector("#add");
const input = document.querySelector("#item");
const list = document.querySelector("#items");

function render() {
  list.replaceChildren();
  for (const item of items) {
    const li = document.createElement("li");
    li.textContent = item;
    list.append(li);
  }
}

form.addEventListener("submit", (event) => {
  event.preventDefault();
  items.push(input.value);
  input.value = "";
  render();
});
const guests = [];
const form = document.querySelector("#guest-form");
const name = document.querySelector("#name");
const count = document.querySelector("#count");
const list = document.querySelector("#guests");

function render() {
  // 2. one li per guest
  // 3. the number of guests in count
}

// 1. on submit: add the typed name, then render
const bills = [];
const names = [];
const form = document.querySelector("#name-form");
const name = document.querySelector("#name");
const add = document.querySelector("#add");
const list = document.querySelector("#names");

function render() {
  list.replaceChildren();
  for (const n of names) {
    const li = document.createElement("li");
    li.textContent = n;
    list.append(li);
  }
}

form.addEventListener("submit", (event) => {
  event.preventDefault();
  names.push(name.value);
  name.value = "";
  render();
});

render();
Terminal
Step 1 of 10Answer a click

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)