JavaScript Event Listener List

Mehvish Ashiq Oct 12, 2023
  1. Get JavaScript Event Listener List
  2. Use the Chrome Developer Tools Console to Get JavaScript Event Listener List
JavaScript Event Listener List

A website has many events that make the web page more interactive. For example, you click a Contact Us button, and it quickly opens a form for you to contact because of event listeners.

This tutorial will learn how to list JavaScript Event Listeners collectively and individually. We can use different methods and techniques using Chrome Developer Tools Console, discussed with respective examples.

Get JavaScript Event Listener List

Let’s understand the basic code (HTML and JavaScript) first.

HTML Code:

<!DOCTYPE html>
<html lang="en">
<head>
 <title>Get a list of event listeners</title>
 <script src="./eventList.js"></script>
</head>
<body>
 <label>First name:</label>
 <input type="text" id="firstname" name="first-name">
 <br />

 <label>Last name:</label>
 <input type="text" id="lastname" name="last-name">
 <br />

 <button id="btn">Submit</button>

</body>
</html>

JavaScript Code:

window.onload = function() {
  document.addEventListener('keypress', e => {
    if (e.key === 'Enter') {
      e.preventDefault();
      return false;
    }
  });

  document.getElementById('btn').addEventListener('click', function() {
    console.log('You have clicked the submit button');
  });
}

Output:

javascript event listeners list - basic output

We have two <input> elements of type text and one button on which we have attached events. There are three events, keypress, click, and onload.

The keypress event gets triggered if you press the Enter key while staying anywhere on the document. The click event gets fired when you click on the Submit button.

And, when the whole page is loaded, the onload event is fired. Now, how can we know about the event listener?

Copy the following JavaScript code to practice.

window.onload = function() {
  document.addEventListener('keypress', e => {
    console.log(e);
    if (e.key === 'Enter') {
      e.preventDefault();
      return false;
    }
  });

  document.getElementById('btn').addEventListener('click', function(event) {
    console.log(event);
    console.log('You have clicked the submit button');
  });
}

Output:

javascript event listeners list - list events individually

If we press the Enter key in the first name or last name text box, it shows KeyboardEvent with properties. And if we click on the Submit button, the PointerEvent listens.

In this way, we know which element has what event listener. You can use console.log(event.target); instead of console.log(event); in the above code if you want to know the element on which a particular event is attached.

We pressed the Enter key on both text fields and clicked on the button. Now, see the following output. The complete HTML elements are printed.

Output:

javascript event listeners list - list elements individually

This case is useful when we know about events but need debugging.

What if we have hundreds of events? How do we know what event is for which HTML element?

Go to the page, Right-Click on the element, and select the Inspect option. Then, select the Event Listeners tab and check a list of events attached to that particular HTML element.

Observe the following output.

Output:

javascript event listeners list - list event listeners collectively with inspect

In the output given above, you may have noted that we have three events listeners: click, keypress, and load when clicking on the Submit button, while keypress and load are for <input> elements.

Use the Chrome Developer Tools Console to Get JavaScript Event Listener List

We can also use the getEventListeners(Object) method on Chrome Developer Tools Console to get event listeners. Check the following output.

Output:

javascript event listeners list - geteventlisteners console function

What if we want to have a complete list of events in one go? You can do this on any website to know which event is listening to what HTML element.

Type the following command on the console.

Array.from(document.querySelectorAll('*')).forEach(e => {
  const ev = getEventListeners(e);
  if (Object.keys(ev).length !== 0) {
    console.log(e, ev)
  }
});

Output:

javascript event listeners list - list event listeners collectively with inspect on any site

The above output gives all event listeners their respective HTML elements side-by-side.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - JavaScript Event