deFreitas
deFreitas

Reputation: 4448

See what events are linked to the element in jQuery

I need to know what codes are being executed when an event is triggered on jQuery.

Common in JavaScript when I add an event it gets saved in their name only variable that jQuery is not in the same place.

example:

 / / Adding code to an event 
window.onclick = function () {alert ('hi!')}; 

/ / To see the event code 
console.log (window.onclick); 

And in jQuery?

Upvotes: 2

Views: 277

Answers (3)

Alexander
Alexander

Reputation: 12795

As of jQuery 1.7 the whole event system was rewritten from the ground up. But you can use jQuery's method that isn't officially documented...

$._data($('selector')[0],'events');

I'd suggest not to use this in your production code .

Upvotes: 1

Vlad Nikitin
Vlad Nikitin

Reputation: 1951

You can view all jquery event handlers in next way:

jQuery._data(document.getElementById('id-of-the-element'))

or

jQuery._data(jQuery('#id-of-the-element')[0])

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82251

Try this:

 // UPDATED -> NOW WORKS WITH jQuery 1.3.1
$.fn.listHandlers = function(events, outputFunction) {
return this.each(function(i){
    var elem = this,
        dEvents = $(this).data('events');
    if (!dEvents) {return;}
    $.each(dEvents, function(name, handler){
        if((new RegExp('^(' + (events === '*' ? '.+' : e    vents.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
           $.each(handler, function(i,handler){
               outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
           });
       }
    });
});
};

And to get click event:

// List onclick handlers of window:
 $(window).listHandlers('onclick', console.info);

Upvotes: 0

Related Questions