1

With so many different devices, doing so many different things I thought it might be useful to create a universal listener. This is my simple attempt that, of course, cannot work. But it may just stimulate someone into coming up with a real solution:-

$("#allEvents").on("*", function(evnt) {
    console.log(evnt.type);
});

<div id="allEvents">
    this is some text inside div allEvents
</div>

Alternatively, is there something out there that can tell a user what events are fired when they do certain things on their devices?

cneeds
  • 319
  • 3
  • 13
  • possible duplicate of [How can I bind all events on a DOM element?](http://stackoverflow.com/questions/5848598/how-can-i-bind-all-events-on-a-dom-element) – Antony Dec 04 '14 at 13:52
  • I'm looking at that @Antony but so far I can't get it to work. I'm obviously missing something critical – cneeds Dec 04 '14 at 14:32

1 Answers1

0

The nearest you can get is to get all the events that a browser supports and put it in a array and then bind to it and check the event.type. For that we will use Object.keys to get the keys of document and then we will filter off to only include events and remove the "on" prefix from them.

Now, we have got an array of events supported by the browser without "on" prefix which we can join with Array.join(" ") and then we can make use of e to check against the type of event.

var arr = Object.keys(document).filter(function(x) {
    return x.indexOf("on") == 0
}).map(function(x) {
    return x.replace("on", "");
});
$('#allEvents').on(arr.join(" "), function(e) {
    console.log(e.type);
});
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • This then assumes that I know what events each (and every) browser supports? I was hoping to come from the POV that I don't know, how can I find out (aside from reading the documentation for every browser and every platform that it supports)? – cneeds Dec 04 '14 at 14:11
  • @cneeds ah! My code will work nearly in every browser as the events are nearly same. – Amit Joki Dec 04 '14 at 14:26
  • Until you start including tabs, notes, i-phones, etc. @Amit – cneeds Dec 04 '14 at 14:28
  • @cneeds hey! javascript is javascript everywhere.. I'm just taking the events which is present in the browser that's executing it so how do you suppose the code will break? – Amit Joki Dec 04 '14 at 14:30
  • No @Amit, that's not what I'm saying. I tried your code in the fiddle and I can't get it to work... – cneeds Dec 04 '14 at 14:37
  • 1
    My code = Take the event from browser. I'm not assuming that you know. `Object.keys(document).filter(function(x) { return x.indexOf("on") == 0 }).map(function(x) { return x.replace("on", ""); });` is the code to get the events from the browser. – Amit Joki Dec 04 '14 at 14:39
  • OK, I see that now, but it's not working:- http://jsfiddle.net/cneeds/p5kcx6c3/1/ – cneeds Dec 04 '14 at 14:41