2

Is there any way I can view the current method assigned to a selects change event. I have tried.

$('#select').change()

but that just returns me the change event. I don't really need to do this but would be very handy for debugging. Save me hunting through the code to find the method, this way I can simply search the text in the method and find the method quickly.

Dan Walmsley
  • 2,765
  • 7
  • 26
  • 45

2 Answers2

2
$('#select').data('events').change;

This will contain an array of objects, one per event handler.


If you're sure there's only one change event handler, you can access the function directly:

var theFunction = $('#select').data('events').change[0].handler;

See it here in action: http://jsfiddle.net/7UD3e/

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 2
    Just for any lurkers, that's not in the documented jQuery API. No problem if you're just debugging, but if you use it for production it's likely to break in 1.8. – Dave Methvin Jan 18 '12 at 20:19
1

A slight alteration to Joseph's answer will make the actual code for the function be output:

var clickEvents = $('#select').data("events").change;
$.each(clickEvents, function(key, handlerObj) {
    console.log(handlerObj.handler) // prints actual function code, does not run function
})

Source: How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)

Here is a demo: http://jsfiddle.net/7UD3e/2/

Community
  • 1
  • 1
Jasper
  • 75,717
  • 14
  • 151
  • 146