[javascript] How to find out which JavaScript events fired?

I have a select list:

<select id="filter">
  <option value="Open" selected="selected">Open</option>
  <option value="Closed">Closed</option>
</select>

When I select Closed the page reloads. In this case it shows closed tickets (instead of opened). It works fine when I do it manually.

The problem is that the page does not reload when I select Closed with Watir:

browser.select_list(:id => "filter").select "Closed"

That usually means that some JavaScript event is not fired. I can fire events with Watir:

browser.select_list(:id => "filter").fire_event "onclick"

but I need to know which event to fire.

Is there a way to find out which events are defined for an element?

This question is related to javascript events watir dom-events browser-automation

The answer is


Just thought I'd add that you can do this in Chrome as well:

Ctrl + Shift + I (Developer Tools) > Sources> Event Listener Breakpoints (on the right).

You can also view all events that have already been attached by simply right clicking on the element and then browsing its properties (the panel on the right).

For example:

  • Right click on the upvote button to the left
  • Select inspect element
  • Collapse the styles section (section on the far right - double chevron)
  • Expand the event listeners option
  • Now you can see the events bound to the upvote
  • Not sure if it's quite as powerful as the firebug option, but has been enough for most of my stuff.

    Another option that is a bit different but surprisingly awesome is Visual Event: http://www.sprymedia.co.uk/article/Visual+Event+2

    It highlights all of the elements on a page that have been bound and has popovers showing the functions that are called. Pretty nifty for a bookmark! There's a Chrome plugin as well if that's more your thing - not sure about other browsers.

    AnonymousAndrew has also pointed out monitorEvents(window); here


    Regarding Chrome, checkout the monitorEvents() via the command line API.

    • Open the console via Menu > Tools > JavaScript Console.

    • Enter monitorEvents(window);

    • View the console flooded with events

       ...
       mousemove MouseEvent {dataTransfer: ...}
       mouseout MouseEvent {dataTransfer: ...}
       mouseover MouseEvent {dataTransfer: ...}
       change Event {clipboardData: ...}
       ...
      

    There are other examples in the documentation. I'm guessing this feature was added after the previous answer.


    You can use getEventListeners in your Google Chrome developer console.

    getEventListeners(object) returns the event listeners registered on the specified object.

    getEventListeners(document.querySelector('option[value=Closed]'));
    

    Examples related to javascript

    need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

    Examples related to events

    onKeyDown event not working on divs in React Detect click outside Angular component Angular 2 Hover event Global Events in Angular How to fire an event when v-model changes? Passing string parameter in JavaScript function Capture close event on Bootstrap Modal AngularJs event to call after content is loaded Remove All Event Listeners of Specific Type Jquery .on('scroll') not firing the event while scrolling

    Examples related to watir

    How to find out which JavaScript events fired?

    Examples related to dom-events

    Detecting real time window size changes in Angular 4 Does Enter key trigger a click event? What are passive event listeners? Stop mouse event propagation React onClick function fires on render How do you Hover in ReactJS? - onMouseLeave not registered during fast hover over iFrame onload JavaScript event addEventListener, "change" and option selection Automatically pass $event with ng-click? JavaScript click event listener on class

    Examples related to browser-automation

    How to find out which JavaScript events fired? How to automate browsing using python?