Skip to content

Instantly share code, notes, and snippets.

@NickCraver
Created April 25, 2015 15:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickCraver/c5a8e14920b282940ffd to your computer and use it in GitHub Desktop.
Save NickCraver/c5a8e14920b282940ffd to your computer and use it in GitHub Desktop.
A simple jQuery plugin to print bound event handlers, including on parent elements
$.fn.logHandlers = function() {
var result = {};
this.parents().andSelf().each(function(_, e) {
var events = $._data(e, 'events') || {};
if ($.isEmptyObject(events)) return;
var cur = result[this.outerHTML.split($(this).html())[0]] = {};
Object.keys(events).forEach(function(k) {
cur[k] = events[k].map(function(ev) {
return ev.handler.toString();
});
});
});
console.dir(result);
// Note: explicitly NOT doing "return this;" as a normal plugin would because the main use is from the console
// return this; would re-print the original elements in their own array, cluttering results
};
// Example usage on Stack Overflow: $(".topbar-icon").logHandlers();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment