Event Trainer — JavaScript

Train event listeners: 12 levels of click, keydown, propagation and bubbling. A JavaScript event handling game for responsive UI.

What you learn in this game

addEventListener attaches a listener: a function that waits for an event and runs when the event happens. The click event happens when the element is pressed. As Mr. Karim says: a big fish is not caught, it is waited for.

The input event happens every time a field's content changes, once per keystroke. The change event only comes once the field is left, so a board waiting for change stays still while someone types.

A sent form has a default behaviour: the browser loads the page again. event.preventDefault() cancels it, so your code handles the data and the page stays put.

A keyboard press goes to the element that has the focus and rises up to document, so a keydown listener usually sits on document. event.key holds the key's name: "Enter", "a", "1".

An event from an li rises to its parent (bubbling). One listener on the parent hears all its children, children added later too, and event.target points at the child that was pressed.

removeEventListener takes a listener off. Its function must be the same function that was attached, so keep it in a variable. A shorter way: the option { once: true } takes it off by itself after one call.

What the levels ask

  1. Attach a click listener to #kentongan: when it is struck, its label becomes "Perahu datang!". Do not strike it yourself; Mr. Karim will.
  2. When #saklar is pressed, add the class nyala to #lampu so the lighthouse lights up. Its own class lampu stays.
  3. Mrs. Lastri types the catch's weight into #berat. Attach an input listener so #papan writes what the field holds every time a digit goes in.
  4. Every boat that moors strikes #kentongan once. Raise jumlah by one on every strike and write it in #hitung, starting from 0. Three boats come home tonight.
  5. When the form #laporan is sent, cancel its default behaviour (the panel loads again and the lighthouse goes dark with it) with event.preventDefault(), then write "Tercatat" in #hasil.
  6. Attach a keydown listener to document: every time a keyboard key is pressed, #layar writes its name from event.key. Mr. Karim will press two keys.
  7. Attach ONE click listener to #perahu, not one on every li. The boat whose name board is pressed (event.target) gets the class sandar and is tied up. Tonight a boat comes in late.
  8. The pier gate has a sensor. When someone comes near (mouseenter), its label reads "Siapa?". When it is pressed (click), it reads "Boleh lewat". Attach two listeners to #palang.
  9. Mrs. Ijah haggles with #tawar: every offer takes 5000 off #harga. But one offer is enough for Mrs. Ijah. Once it has been used, the button stops listening: { once: true } or removeEventListener.
  10. One click listener on #peti: the crate pressed writes what it holds, its data-ikan, in #timbangan. Take it from event.target.dataset. Tonight one more crate comes off a boat.
  11. The pier has an alarm: #dermaga writes "Ada orang di dermaga!" in #kabar, the alarm drum writes "Perahu datang". The alarm drum hangs on the pier, so its strike rises to the alarm too. Stop it with stopPropagation in the alarm drum's listener.
  12. Mrs. Lastri's catch book: when the form #catat is sent, stop the reload, make an li holding what #ikan holds, add it to #buku, then empty the field for the next fish.