You can then check if the clicked element has the selector you care about. (From: Learning JavaScript) For example: You could add a separate event listener for every single accordion link on the page, but that would be madness. Events are said to be an essential part of the JavaScript. Definition and Usage. Making the event delegation work requires 3 steps: Determine the parent of elements to watch for events; Attach the event listener to the parent element The addEventListener() method attaches an event handler to the specified element. Events can be listened for by using addEventListener or inline methods such as onclick. To add the event listener to the multiple elements, first we need to access the multiple elements with the same class name or id using document.querySelectorAll() method then we need to loop through each element using the forEach() method and add an event listener to it. An event listener is a procedure or function in a computer program that waits for an event to occur; that event may be a user clicking or moving the mouse, pressing a key on the keyboard, or an internal timer or interrupt. A web page responds according to the event that occurred. The handleEvent method can be used for multiple events. There's an easier way: Event Bubbling. Two major differences between the older model and the newer DOM Level 2 event model is that 1) the newer model isnât as dependent on a specific event handler property, and 2) you can register multiple event handler functions for any one event on any one object. Events can be user-generated or generated by API's. This mechanism is named event propagation. In other words, how to call addEventListener() on multiple elements at the same time? A âkeydownâ event listener looks like this: document.addEventListener('keydown', (event) => { // do something }); When we use a âkeydownâ event we are listening for the moment when a key is pressed. We can similarly react when a key is released using the âkeyupâ event. An alternate approach is to listen for all clicks on the page by attaching your event listener to the document element. The addEventListener() method is an inbuilt function of JavaScript. Tip: Use the removeEventListener() method to remove an event handler that has been attached with the addEventListener() method. Event phases are capture (DOM -> target), bubble (target-> DOM) and target. Using a loop The loop is the simplest one conceptually. Adding event listener to multiple elements. The listener is in effect a loop that is programmed to react to an input or signal. You can specify logic by using event.type. An event listener is a JavaScript's procedure that waits for the occurrence of an event. Re: addEventListener function calling multiple times for event tracking â05-25-2018 08:22 AM - last edited on â05-28-2018 09:26 AM by kathleen_jo â05-25-2018 08:22 AM The event delegation is a useful pattern because you can listen for events on multiple elements using one event handler. One is using a loop, the other is using event bubbling. addEventListener can add multiple events, whereas with onclick this cannot be done. What is an Event Listener? Take a look at the example below: Take a look at the example below: You can do this in 2 ways. addEventListener() and removeEventListener() The newest type of event mechanism is defined in the Document Object Model (DOM) Level 2 Events Specification, which provides browsers with a new function â addEventListener().This functions in a similar way to the event handler properties, but the syntax is obviously different. The JavaScript addEventListener() method lets you add multiple event handlers to a single element, while not overwriting any previously assigned event handlers. Tip: Use the document.addEventListener() method to attach an event handler to the document. But how can you attach the same event to multiple elements?