OnlineCBT Logo OnlineCBT
JavaScript Tutorial
JAVASCRIPT · LESSON 20

Event Handling: addEventListener and Event Object

By Ratan Updated: 16 Oct 2026

Respond to user actions like click, submit, input, keydown, and mouseover using event listeners.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Register behavior without running it immediately

addEventListener associates an event type with a callback. The callback receives an event object describing the dispatch. Prefer actual interactive controls so keyboard users receive their native behavior too.


0

Each activation increments the display. The console logs click and true. Pass handleClick, not handleClick(), because the latter would invoke it during registration.

target versus currentTarget

target identifies the originating/retargeted event target. currentTarget is the element whose listener is currently running. Clicking an icon inside a button may make target the icon while currentTarget remains the button. Read a reference to currentTarget during the handler if you need it later in asynchronous code.

Listener lifetime

button.removeEventListener('click', handleClick);

Removal needs the same callback identity and matching capture setting. A new arrow that looks identical is a different function. For coordinated cleanup, supported event listeners can use an AbortController signal and be removed by aborting it.

Useful options

once:true removes a listener after its first invocation. capture:true selects the capture phase. passive:true promises the listener will not call preventDefault; do not mark a listener passive if it must cancel the browser's default action.

Practice

  1. Add a reset button with its own handler.
  2. Place a span inside Add one and compare target/currentTarget.
  3. Register a once listener and observe that it fires once.
  4. Remove a listener with its original function reference and confirm the display no longer changes from it.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat