Skip to curriculum
Jump to the editor

17.9. Stop propagation, prevent default and listen once

Easy
Topics
15–25 min

Four everyday click problems, each solved by one tool of the event system. A preview link should open a popup instead of navigating. A dropdown closes when the page hears a click, but clicks inside it must not count. A welcome tip should show on the first click only. A Pay button must refuse clicks while the terms are not accepted, before any other code reacts.

Write the four helpers in events.js. Each one adds listeners; none of them replaces or copies the element.

Signature

JAVASCRIPT
blockNavigation(link)              // returns nothing
keepClicksInside(panel)            // returns nothing
onFirstClick(button, handler)      // returns a cancel function
guardClicks(button, isAllowed)     // returns a remove function

Requirements

  1. blockNavigation: a click on the link is cancelled (its default action, following the link, does not happen), but it still bubbles: ancestors hear it and see event.defaultPrevented as true. The link's other listeners, added before or after, still run. Other links are not affected.
  2. keepClicksInside: bubbling listeners above the panel never hear clicks from inside it. Capture listeners above it are different: they run on the way down, before the click reaches the panel, so they still hear it (stopping propagation cannot undo a delivery that has already happened). Listeners on the panel itself (capture or bubbling, added before or after) and inside it still run. Default actions still happen (a checkbox inside still toggles). Clicks outside the panel and other event types are not affected.
  3. onFirstClick: handler runs on the first click only, with the click event and this set to the button. It still runs only once when it throws, or when it clicks the button again itself. The returned function cancels it if it has not run yet (calling it again is harmless). Two buttons work separately.
  4. guardClicks: on every click, isAllowed(event) is asked before the button's other click listeners: its bubbling listeners, including ones added before guardClicks, and its capture listeners added after it. When it returns false, the click is cancelled and none of those listeners runs, nor any bubbling listener on the button's ancestors; a guarded checkbox does not toggle. When it returns true, the click is untouched. The returned function removes the guard.

Examples

JAVASCRIPT
blockNavigation(previewLink);
previewLink.addEventListener("click", () => openPopup(previewLink.href));
// a click opens the popup; the page stays where it is
JAVASCRIPT
keepClicksInside(dropdown);
document.addEventListener("click", closeDropdown);
// clicks inside the dropdown do not close it; clicks elsewhere do
JAVASCRIPT
onFirstClick(helpButton, function () { this.textContent = "Thanks!"; });

const stop = guardClicks(payButton, () => terms.checked);
payButton.addEventListener("click", pay);
// with the box unticked, a click on Pay does nothing at all

Environment

  • Plain JavaScript in a browser page; the tests call the four global functions on elements they build and dispatch real click events (and call element.click() for checkboxes).
  • Capture listeners run before the guard when other code added them on the button's ancestors, or on the button itself before guardClicks was called. No listener can stop a listener that has already run, so those are out of scope for guardClicks.
  • Any addEventListener options are allowed.

Hints

  1. Which method cancels the browser's action, which one stops the event travelling further, and which one also skips the other listeners on the same element?
  2. On the element that was clicked, which listeners run first: capture or bubbling ones?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…