Skip to content

Instantly share code, notes, and snippets.

@lewang
Created February 15, 2012 05:50
Show Gist options
  • Save lewang/1833599 to your computer and use it in GitHub Desktop.
Save lewang/1833599 to your computer and use it in GitHub Desktop.
show all jQuery events bound to element
// usage: $("a.connectoin_action").getAllHandlersFor("click")
$.fn.getAllHandlersFor = function (event) {
var results = [],
$orig = $(this),
$next,
handlers;
handlers = $orig.data("events") && $orig.data("events")[event];
if (handlers) results.push([$orig, handlers]);
$next = $orig.parent();
while ($next.length) {
var all_handlers = $next.data("events") && $next.data("events")[event];
handlers = [];
if (all_handlers) {
$.each(all_handlers, function(index, handler) {
if (handler.selector && $orig.is(handler.selector)) {
handlers.push(handler);
}
return true;
});
}
if (handlers.length) {
results.push([$next, handlers]);
}
$next = $next.parent();
}
console.log(results);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment