How can I add an event listener for all events in javascript without listing them individually?
There's no wild card, but using jQuery you can have 1 long event listener line, rather than multiple.
How do you log all events fired by an element in jQuery?
Like so:
('body').on("click mousedown mouseup focus blur keydown change dblclick mousemove mouseover mouseout mousewheel keydown keyup keypress textInput touchstart touchmove touchend touchcancel resize scroll zoom select change submit reset",function(e){
console.log(e);
});
There is no feature to add a *
listener, and I doubt if you want hundreds of events on an element being listened for anyway.
By the way, instead of your roll-your-own architecture for a generic event handler in the form of your myObject.controller
, you should use the under-appreciated approach involving the EventListener
interface and the handleEvent
method it defines.
A little late to the party, but here's how I add all event listeners & log them to the console:
Object.keys(window).forEach(key => {
if(/./.test(key)){
window.addEventListener(key.slice(2), event => {
console.log(key, event)
})
}
})