← Back to JavaScript
25

Events in real interfaces: from basic clicks to smart delegation

Learn how to design maintainable interactions with `addEventListener`, the `event` object, `preventDefault`, propagation control and delegation for dynamic lists.

📘 Theory

A useful event model: trigger, surface, logic and result

Before writing code, define the interaction as a clear rule.

A good sentence is: when X happens on Y, run Z and update W. That structure keeps event code readable even as the interface grows.

Not every event expresses the same intention. `input` is continuous feedback, while `change` and `submit` usually mean the user has finished a step.

1

Trigger

What happens exactly.

  • click
  • submit
  • keydown
2

Surface

Where you listen.

  • button
  • form
  • delegated container
3

Logic

What your code decides.

  • validate
  • toggle
  • route an action
4

Result

What changes in the UI.

  • update text
  • disable a button
  • remove an item

`addEventListener` with architectural intent

Listen where it makes sense, not where the first selector happens to work.

`addEventListener` keeps markup and behavior separate, and it lets you attach more than one listener to the same node without collisions.

In small components, it helps to cache references in a `ui` object and register them inside a `bindEvents()` function.

  • Name handlers by intention, such as `onSaveClick`.
  • Avoid inline listeners in HTML.
  • Group event binding to make teardown and testing easier.

Understanding `event`: target, currentTarget and default behavior

Many event bugs come from reading the wrong property.

1

`event.target` is the node that originated the event. `event.currentTarget` is the node that owns the listener. Delegation depends on understanding that difference.

2

When a button contains an icon or nested text, the click may start on that child node. That is why `closest()` becomes such a practical tool.

`preventDefault()` and event flow control

Control which native action should run and when your own logic should interrupt it.

1

`preventDefault()` stops the native browser action, such as form submission or link navigation. Use it when you need to validate, confirm or transform data first.

2

`stopPropagation()` can also help in specific cases, but use it carefully so you do not hide useful events from higher layers of the UI.

🧪 Learn by doing

Example Guided example: a keyboard shortcut for search Listen globally to `keydown` and react only to `Ctrl + K`, with automatic focus on the search field.
Example Interactive demo: a mini task board Add tasks and mark them done or remove them with a single delegated listener.

🏁 Challenges

Challenge Challenge: click and double click on the same button Register both events and update a status paragraph with a different message for each case.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com