Lazo Lab Sign up

Web Development / JavaScript

Lesson 21 of 30

Events

Events fire when users click, type or submit. addEventListener connects a function to an event.

Key points

  • click, input, submit, keydown
  • e.preventDefault() stops the default action
  • Listen on the element that matters
document.querySelector("#btn").addEventListener("click", () => {
  alert("Clicked!");
});
Watch a video on thisOpens YouTube search results for “Events” in a new tab

Quiz · +10 XP

Which method connects a function to a click?

Log in to save progress and earn XP.