JavaScript HTML DOM EventListener


The addEventListener() method

The addEventListener() method links a function to a chosen element for handling events.

The addEventListener() method adds an event handler to an element without replacing any existing event handlers.

You can attach multiple event handlers to a single element.

You can attach multiple event handlers of the same kind to a single element, like having two "click" events.

You can attach event listeners to any DOM object, not just HTML elements, such as the window object.

The addEventListener() method helps in managing how an event responds during bubbling.

When you employ the addEventListener() method in JavaScript, it helps organize your code by separating it from the HTML markup. This enhances readability and enables you to incorporate event listeners, even in situations where you lack control over the HTML markup.

You can quickly delete an event listener with the removeEventListener() method.


Syntax

element.addEventListener(event, function, useCapture);

The initial factor is the event type (such as "click" or "mousedown" or any other HTML DOM Event.)

The second part is the action we want to happen when the event takes place.

The third part is a true/false value that tells whether to use event bubbling or event capturing. You don't have to include this part if you don't need it.

Make sure not to use the on prefix when referring to the event. Instead of using onclick, use click.


Add an Event Handler to an Element

You may also look up an external function with a specific name.


Add Many Event Handlers to the Same Element

The addEventListener() method lets you attach multiple events to a single element without replacing the current events.

You can attach various types of events to a single element.


Add an Event Handler to the window Object

The method called addEventListener() lets you connect event listeners to various things on a webpage, such as elements, the document itself, the window, or other objects that can do things when events happen, such as the xmlHttpRequest object.


Passing Parameters

When you pass values for parameters, employ an 'anonymous function' that invokes the designated function along with the specified parameters:


Event Bubbling or Event Capturing?

In HTML DOM, events can be propagated in two ways: bubbling and capturing.

Event propagation is how we determine the order of elements when an event happens. For example, if there's a <div> containing a <p> element, and the user clicks on the element, we need to decide which element's "click" event should be dealt with first.

In bubbling the event of the innermost element is dealt with before the outer one. For example, in the given scenario, the click event of the element is processed before the click event of the <div> element.

When capturing events in HTML, the outermost element's event is processed before the innermost. For example, if there is a <div> element inside a <p> element, the click event for the <div> will be processed first, followed by the click event for the element.

You can define how an event propagates using the "useCapture" parameter with the addEventListener() method.

addEventListener(event, function, useCapture);

The initial setting is set to false, meaning it employs the bubbling propagation. If the value is changed to true, the event will utilize the capturing propagation instead.


The removeEventListener() method

The removeEventListener() function eliminates event handlers linked using the addEventListener() function.