I'm looping through the properties of an HTMLElement
and doing some things with the element's event names. Do HTMLElement
event properties always begin with "on"? My approach is working fine at the moment, but I would like to know if it is reliable:
for (let key in elem) {
// check if hasOwnProperty is false to make sure the property isn't spoofed
if (!elem.hasOwnProperty(key) && key.indexOf("on") === 0) {
const eventName = key.substring(2);
// do stuff here with eventName
}
}
Note: The only event name I can think of that doesn't follow the convention is DOMContentLoaded
, but it's an event on the Document
object, not HTMLElement
.