devlife
devlife

Reputation: 16145

How to find where jquery event handlers are assigned?

For some reason I have an additional change handler on a number input. I can't for the life of me figure out where it is coming from. Can anyone point me in the direction of how I can track down this rouge event handler assignment? In chrome dev tools if I inspect the element and look at the event handlers area it just points to (1) jquery and (2) undefined.

Upvotes: 1

Views: 387

Answers (3)

devlife
devlife

Reputation: 16145

These answers weren't quite what I was looking for but I was able to find out where the handlers were coming from. I'm using webshims and a jquery tool called overlay. Apparently the overlay executes the code in the script tag when the page is rendered and again when the overlay is actually applied.

Upvotes: 0

abuduba
abuduba

Reputation: 5042

All of events binded by jquery are in $( target ).data( 'events' ); That represents object with keys as name of event contains array of functions.

For example try this in console on stackoverflow directly: $('a').data('events').click[0].handler;

To list each of event type calbacks assigned by jquery object try:

$.each( $( youSelector ).data('events'), function(v,k){
   console.log( v +":");
   $(this).each( function(){
     console.log( this.handler );
   });
   console.log("------------------");
});

Way to find out from which file they came is search from piece of handler code in console on search input in resources tab :)

Upvotes: 1

gion_13
gion_13

Reputation: 41533

If the other handler is undesired, you can always use unbind :

$("#id").unbind('change').bind("change", handler);

Of course, you'll have to take into consideratin every possible case, so a complete solutin would be :

$("#id")
    .off('change')
    .die('change')
    .unbind('change')
    .on("change", handler);

Upvotes: 1

Related Questions