Перейти к содержимому

Как посмотреть event listener в chrome

  • автор:

Chrome DevTools: Show the function definition of registered event listeners on any element

DevTools can show you JavaScript event listeners which are registered on elements. For example a click handler registered with .addEventListener() .

DevTools can also take you to the exact line of code where the event is registered, which makes it useful when debugging from the outside in.

How to use this feature

  1. Inspect an element in the Elements Panel
  2. Navigate to the Event Listeners Pane in the Elements Panel
  3. Browse the events registered on any node. You can view the source of an event listener by selecting Show Function Definition in the context menu

Reminder

As a heads-up, browser extensions often apply event listeners to elements in the page, so you may see code from other sources when browsing the Event Listeners Pane.

Extra resources

I've added additional information on debugging event listeners in DevTools in the View and remove JavaScript event listeners tip.

How to find Event Listeners on a DOM Node when Debugging

Say you want to find all the event listeners attached to a DOM node. How do you go about it?

Solution 1: Google Chrome

This solution will work only on Google Chrome or Chromium-based browsers.

We are going to use this website as an example.

Let’s say you wanted to find all the event listeners attached to the search icon DOM element.

Picture of the author

Right-click on the search icon button and choose "inspect" to open the Chrome developer tools.

Picture of the author

Once the dev tools are open, switch to the "Event Listeners" tab and you will see all the event listeners bound to the element.

Picture of the author

You can expand any event listener by clicking the right-pointing arrowhead.

When you expand an event listener and hover over the element, the "remove" button will appear. It can be used to delete the event listener.

Picture of the author

Clicking on the filename main.min.js:6 takes you directly to the event listener callback function source by automatically switching you to the "sources" tab.

Picture of the author

As you can see in the screenshot below, you have been taken to the exact location of the event listener callback function.

Picture of the author

We can’t see the event listener code properly right now since the code is minified.

Click on the <> icon to format the code.

Picture of the author

Solution 2: Firefox

Let’s say you wanted to find all the event listeners attached to the search icon DOM element on this website.

Picture of the author

Right-click on the search icon button and choose "inspect" to open Firefox developer tools.

Picture of the author

The "search icon" button DOM node will be automatically highlighted in the Firefox "Inspector" tab.

Picture of the author

Click on the word "event" on the highlighted DOM node to see all the events bound to the element.

Picture of the author

Click on the right-pointing arrowhead to expand the event listener. You will be shown the source code for the event listener.

Picture of the author

Picture of the author

Hey there, I am Stanley. I like to publish tutorials about the frontend, and backend. I am working towards becoming a full stack developer.

© Copyright 2019—2023. Built with passion by Stanley Ulili in Zomba, Malawi.

How to find an event listener in Chrome Dev Tools?

I know that hovering over an element causes another element to get a new class added to it. How do I check what and where this eventListener is on Chrome? The Event Listeners tab in Chrome just list some listeners on the document instead of any specific element.

3 Answers 3

To view event listeners for a single element you have selected, make sure you uncheck the ‘Ancestors’ box in the event listeners tab. If checked, you will see all event listeners for the ancestors of that element — which may be what you are seeing now.

Hovering doesn’t really add another class to an element, it triggers a hover state for the element which can be targeted with a CSS pseudo-class selector. This is not an event listener but there are event listeners that can be added to detect the mouse pointer over the element (see end of answer). Here is an example of targeting the hover state of any paragraph tag with the :hover pseudo-class selector:

In Chrome DevTools, you can force a hover state on the element to view its hover state behavior. To do this click the :hov button at the top right of the styles pane, and then check the :hover box. If there are any pseudo-class :hover styling rules for that element, they will now be displayed in the styles pane and the element will change accordingly in the viewport.

There are event listeners such as ‘mouseover’ and ‘mouseout’ which, when used together, will emulate the hover state behavior. Note, however, that these are independent of the hover state — so forcing a hover state in DevTools will not cause these event listeners to trigger. If these event listeners are present on the element you are inspecting, they will show up in the event listeners tab — just remember to uncheck the ‘Ancestors’ box if you are inspecting a page with a lot of event listeners.

Chrome: Как найти событие установленное на кнопку или ссылку

Последнее время я работаю только с бекендом разнообразных проектов и к фронту и тому что там происходит обращаюсь все реже и реже. А происходит там довольно много интересного, фронтенд обвешивается разного рода фреймворками по типу vue.js, лоадерами по типу requirejs и библиотеками, все это минимизируют в пару файлов и когда возникает необходимость быстро разобраться где нужный участок кода становится очень сложно, особенно когда ты только начинаешь работать с новым проектом и еще не знаком с его структурой. Очень помогает справиться с этой проблемой отладчики встроенные в браузере. Сегодня, я решил сделать для себя заметку о том, как найти код навешенный на кнопку или ссылку, и начать его дебажить..

Для фронтенд разработчиков, это наверное ежедневная задача, но я сталкиваюсь с ней от случая к случаю, поэтому вечно забываю последовательность того, как я это делаю.

Для начала создадим в jsfiddle кнопку и с помощью jquery навесим на нее несколько событий.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *