If I attach a click event handler:
$(".selector").bind("click", function () {
// some handler function
});
How can I get a reference to that function? This doesn’t work:
var refToFunc = $(".selector").bind("click");
typeof refToFunc === "object"; // I want the function
I think bind("eventname") in that case just returns the jQuery object and not the event handler function. It must be stored somewhere.
Very interesting question. You can retrieve it like this:
Note that we used
[0]because you have an array of handlers, in case you bound more than one handler. For other events, just change to the event name.EDIT
In general, you could use the following plugin to get all handlers of the selected elements for an event (code not optimized yet):
How to use it?:
And it would return an array of functions containing all event handlers.
For your specific question, you could use this plugin like this:
Hope this helps. cheers