Attaches a callback function for a named event to any HTMLElement.
on(name, callback)'click', 'hover', etc.addEventListener method, which also allows to use more specific options.'click' events) are independent of this method, and do not require it to be present in the library.let foo document.getElementById('foo'); foo.on('click', function(e) { console.log(e); });
Using arrow functions can makes this even more lightweight:
let foo document.getElementById('foo'); foo.on('click', e => { console.log(e); });
If you need to be able to unregister the event handler using the .off method, you need to keep a reference to the callback function, as in the following example:
let callback = function(e) { console.log(e); } let foo document.getElementById('foo'); foo.on('click', callback);