Rullst AcademyRullst Academy/Lesson
JavaScript and interaction · Lesson 07 / 14

Events that listen to people

A click, a key and a form submission can express the same intention.

Learning paths14 min

Let's discover together

  1. 1

    addEventListener registers a function for a future event. Pass the function, not its result: button.addEventListener("click", handleClick). Writing handleClick() calls it immediately.

  2. 2

    A native button responds to keyboard and pointer. Do not recreate a button with a div and click listener. Listening for submit on the form also covers Enter submission when the form supports it.

  3. 3

    event.preventDefault() prevents the default action of that cancelable event, such as navigating on form submission. It does not automatically stop propagation or provide security validation. Use it when JavaScript takes over the behavior.

  4. 4

    Register listeners once or remove old ones when reorganizing the page. Two listeners can run an action twice. The project rebuilds buttons with their own listeners and registers the form only once.

From the example to your own interaction

Download this activity kit, extract it and open index.html in a browser. Edit lesson.js, save and reload. You do not need Node or packages. README explains local HTTP when needed.

The demonstration uses supplied JavaScript. It does not execute your editor code. Download your edit separately to test in the local kit.

My files and GitHub
Try the project we will build

Small mission planner

Create, complete and review a study plan using fictional data. Missions stay only in this page’s memory and disappear on reload.

Use a short title without personal data and whole minutes from 1 to 120. Up to 20 missions.

Missions in the plan
0
Minutes across all missions
0
Completed missions
0

No missions in this filter. Add one or choose another filter.

    Suggestion lab

    Scenarios are simulated in this browser by default. No suggestion request is sent and your plan is not replaced.

    Available in the kit opened at http://127.0.0.1:8768. See README; no external account is needed.

      This JSON contains only missions, not your code. The demonstration does not import files or synchronize devices. Download edited JavaScript from the lesson file workspace.

      Official links and next step

      ✦
      YOUR MISSION

      Use addEventListener and submit without losing keyboard operation.

      Now it's your turn

      Discovery workshop

      Think before running

      form.addEventListener("submit", event => { event.preventDefault(); count += 1; });
      Build your answers and check when you are ready.
      A moment to understand

      Which call registers the callback?

      I need a hint

      Predict the result, run your file and explain the difference.

      Loading your journey…

      Take it with you

      Activate the button with click, Enter and Space. Check the count; then register two distinct listeners and explain the difference.

      At your own pace

      My files and GitHub

      Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

      Student guide

      The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

      Checking your access to this lesson…

      Rullstpetbot

      AI can make mistakes. Check the lesson material.