LearnThatStack Ace your next interview
JavaScript · question
Question 16 of 126

What are the different ways to handle events in JavaScript?

beginner
← All JavaScript questions
Re-explain

There are three ways: an inline HTML attribute such as onclick, a DOM property such as button.onclick, and addEventListener(). Use addEventListener() in modern code.

An inline attribute mixes JavaScript into the HTML as a string, and a strict Content Security Policy (CSP) blocks it. A DOM property keeps the code in JavaScript, but each element holds only one handler per event, so assigning a second one replaces the first.

addEventListener() lets you attach many listeners to the same event. It also accepts options such as once and passive, supports the capture phase, and pairs with removeEventListener() for cleanup.

<!-- HTML attribute -->
<button onclick="handleClick()">Click</button>
// DOM property
button.onclick = function() { };
// addEventListener (recommended)
button.addEventListener('click', function() { });
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How do you remove a listener you added, and why does removeEventListener sometimes do nothing? It needs the same function reference and capture flag. A fresh anonymous function never matches, so keep a reference or pass an AbortController signal.
  • What does the passive option do, and when would you set it? It promises the listener won't call preventDefault, so the browser can scroll without waiting for it. Use it on touch and wheel listeners.
  • What are the capture and bubble phases, and how does addEventListener pick one? An event travels down from window to the target, then back up. The third argument or {capture: true} runs the listener on the way down.
  • You have 500 list items that each need a click handler. How would you set that up? Put one listener on the parent and use event.target.closest() to find the clicked item. It also covers items added later.
  • What is `this` inside an event listener, and does an arrow function change it? In a regular function, this is the element the listener is on, same as event.currentTarget. An arrow function keeps this from the enclosing scope.

What you can say

  1. There are three ways to handle an event: an inline HTML attribute like onclick, a DOM property like button.onclick, and addEventListener, which is what I'd use in modern code.
  2. The inline attribute puts JavaScript into the HTML as a string, so markup and logic get mixed, and a strict Content Security Policy blocks it.
  3. The DOM property keeps the code in JavaScript, but an element holds one handler per event, so assigning a second one replaces the first.
  4. addEventListener lets several listeners share the same event, and it takes options like once and passive, plus a flag to listen during the capture phase.
  5. One catch with cleanup is that removeEventListener needs the exact function you added, so you can't remove an anonymous function that way.

Weak answers to avoid

  • Stops at listing the three syntaxes Naming onclick, .onclick and addEventListener is recall. Say why addEventListener wins: many listeners, options like once and passive, capture, and clean removal.
  • Thinks assigning onclick twice runs both The second assignment replaces the first, and that is a classic bug. Say the property holds one handler while addEventListener adds another one each time.
  • Removes a listener with a new anonymous function removeEventListener matches by reference, so a new function matches nothing and the listener stays. Keep a named reference, or use once when it should fire a single time.
  • Recommends inline onclick for simple cases It puts code in the markup as a string, and a strict CSP blocks it. Say you keep handlers in JavaScript even for a single button.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime