EventTarget

EventTarget.prototype.addEventListener

Registers an event handler of a specific event type on the EventTarget

Syntax

JavaScript
target.addEventListener(type, listener, options?)

Parameters

ParameterTypeDescription
typestringThe event type to listen for
listenerEventListener | FunctionThe handler function for the event
optionsboolean | AddEventListenerOptionsOptions specifying characteristics about the event listener

Return Value

undefined

Examples

Basic Usage
const btn = document.querySelector('button')!
btn.addEventListener('click', () => {
  console.log('Button clicked')
})
Practical Example
const input = document.querySelector('input')!
input.addEventListener('input', (e) => {
  const target = e.target as HTMLInputElement
  console.log('Value:', target.value)
})
Advanced Usage
const controller = new AbortController()
window.addEventListener('resize', () => {
  console.log('Resized:', window.innerWidth)
}, { signal: controller.signal })
// Later: controller.abort() to remove

Understanding EventTarget.prototype.addEventListener

The EventTarget.prototype.addEventListener method in JavaScript registers an event handler of a specific event type on the EventTarget. It belongs to the EventTarget object and is one of the most widely used methods for working with eventtarget values in modern JavaScript and TypeScript applications.

The method signature is target.addEventListener(type, listener, options?). It accepts 3 parameters: type, listener, options. When called, it returns undefined. Understanding when and how to use addEventListener() helps you write more expressive, readable code.

Common use cases for EventTarget.prototype.addEventListener include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like event-removeeventlistener, event-dispatchevent, event-preventdefault, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for EventTarget.prototype.addEventListener is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More EventTarget Methods

Other methods in the EventTarget object

Related Tools

More EventTarget Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.