Skip to content
dreamcode
dreamcode
Map
Events
Lesson 36 of 48
+15 XP on finish
JS WEB APISChapter 7 · JS Web APIs

Events and Listeners

Pages respond to people through events. element.addEventListener("click", handler) runs handler every time the element is clicked. The handler receives an event object with details such as event.target, the element that was clicked. After you press Run, click the buttons in the page below: your handlers run for real. You can also trigger a click from code with element.click().

Worked example

How it reads

  • The handler does not run when you add it, only when the event happens
  • launches lives outside the handler, so it remembers the count between clicks
  • button.click() fires a real click event from code
Cloud tip: Pass the function itself, addEventListener("click", launch), not launch(). The parentheses would call it once, immediately.
index.js
JAVASCRIPT
real JavaScript, runs in the live page below
preview · index.htmllive page, click around after you run
Console
Run your code to see its output here.
YOUR TURN

Make the Reset button set the count back to 0 and log reset. Test it by clicking Reset in the page, or by calling .click() on it from your code.

Press Run to check your work.