Sikau

JavaScript · Day 45 · about 30 min

Events

Clicks and key presses.

Pages that listen

Pages come alive when they react: a click, a key press, a mouse hovering. Each of those is an event, and JavaScript can listen for them.

addEventListener

const button = document.querySelector("#like");
const count = document.querySelector("#count");
let likes = 0;

button.addEventListener("click", () => {
  likes++;
  count.textContent = likes;
});

A doorbell: addEventListener is like wiring a doorbell: "when someone presses this, play this tune". The tune doesn't play when you wire it — only when it's pressed.

The rest of this lesson

  • The event objectexplain
  • Events you'll use mostexplain
  • Predictpredict
  • Pick the eventmatch
  • Your turnwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript