
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
NodeJS - eventTarget.addEventListener() Method
The eventTarget.addEventListener() method is used to add a specified listener on the EventTarget it is called on.
The EventTarget class is not directly available in Node.js. Instead, you can use the EventEmitter class from the events module to achieve similar functionality.
This method belongs to EventTarget class of node:events module.
Syntax
Following is the syntax of the NodeJs eventTarget.addListener() method −
eventTarget.addEventListener(type, listener [, options])
Parameters
This method has three parameters described below.
- type: This parameter holds a string value that represents the event type to listen for.
- listener: The object that receives a notification when an event of the specified type occurs.
- options: (Optional)
- once: A Boolean value that indicates that the listener should be called at least at most once after being added. If it is true, then the listener would be automatically removed when it is invoked.
- passive: A Boolean value is true it serves as a hint that the listener wont call the Events objects preventDefault() method.
- capture: A Boolean value that indicated that the events of this type will be dispatched to the specified listener before being dispatched to any EventTarget. This is not directly used in Node.js, it is just added for API completeness.
- signal: Whenever the specified AbortSignal objects abort() method is called the listener will be removed.
Return value
This method returns the event target. It allows adding multiple handlers for an event and provides control over when the listener is activated.
Example 1
Following is the basic example of the NodeJs eventTarget.addEventListener() Method.
First, we imported the node:events module. Then we passed a listener function with an event type foo to the eventTarget.addEventListener() method.
const { EventEmitter, listenerCount } = require('node:events'); function handler(event){ }; const target = new EventTarget(); target.addEventListener('foo', handler);
Output
As per the above program, we added one listener handler to the event type foo.
Example 2
In this program, we are adding two listener functions with an event type foo to the eventTarget.addEventListener() method.
const { EventEmitter, listenerCount } = require('node:events'); function handler1(event){ }; const handler2 = { handleEvent(event) { console.log(event.type); // prints foo to the console } }; const target = new EventTarget(); target.addEventListener('foo', handler1); target.addEventListener('foo', handler2);
Output
As per the above program, we added two event listeners handler1 and handler2 to the event type foo.
Example 3
Note: Any specified listener is added only once to the type event and per capture optional parameter value.
Initially, we imported the node:events module. Then we added a single listener function handler for the foo event.
const { EventEmitter, listenerCount } = require('node:events'); function handler(event){ }; const target = new EventTarget() target.addEventListener('foo', handler, {capture: true}); target.addEventListener('foo', handler, {capture: false});
Output
As per the above program, the listener handler can be added once with capture: false value and capture: true value.
Example 4
Note: In nodejs, we cannot use the event target directly to generate the output, we typically use EventEmitter to handle events instead of the EventTarget used in the browser.
In this example, we imported the node:events module by using EventEmitter instead of EventTarget. Then we passed a listener function with an event type foo. The on method is used to add an event listener and emit method triggers the event.
const { EventEmitter } = require('events'); function handler(event) { console.log('Event received:', event); } const target = new EventEmitter(); target.on('foo', handler); target.emit('foo', { data: 'Some event data' });
Output
The above program produces the following output −
Event received: { data: 'Some event data' }