javascriptevents

How to list all events for html elements?


I'm looking for a way to list all events in Javascript for dynamic event creation. Or, a way to check if a string is a correct event name.


Solution

  • Different events are valid for different nodes.

    const elements = [
      'a',
      'window',
    ];
    
    elements.forEach(elementName => {
      const el = document.createElement(elementName);
      console.log(`======= Events for ${elementName}:`);
      for (var prop in el) {
        if (prop.startsWith('on')) {
          console.log(prop);
        }
      }
    });