2

Is there anyway that I can check if an existing element has event handlers attached to it? Suppose a simple javascript method adds a click event to some div.example (sans jquery), so there are not attribute tags, is it possible to then, using jquery, to capture the event that was attached? I tried to accomplish this using the code below to no avail:

$( '.example' ).each( function() {
var newObj = $(this);
$.each($(newObj).data("events"), function(i, event) {
    alert(i);
        $.each(event, function(j, h) {
            alert(h.handler);
        });
});

});

stavarotti
  • 582
  • 5
  • 19
  • 2
    There is no need to do `$($(this))`. – pimvdb Jul 15 '11 at 17:31
  • You can get all events (and the associated handlers) using the method described [here](http://stackoverflow.com/questions/1515069/jquery-check-if-event-exists-on-element). – George Cummins Jul 15 '11 at 17:33
  • 2
    Also, what's `$('#...').each` supposed to do? There is hopefully only one ID per document. – pimvdb Jul 15 '11 at 17:34
  • And, it works for me: http://jsfiddle.net/qQL38/. You just missed the last `});` of the first `.each`. – pimvdb Jul 15 '11 at 17:35
  • 1
    $(this).data('events') only holds events that were bound using jQuery. – John Strickler Jul 15 '11 at 17:36
  • I thought jQuery's .data() implementation is specific to jQuery. if another batch of javascript attaches a function to an element without using an attribute or jQuery, you'll have to find it using plain javascript I think. – DefyGravity Jul 15 '11 at 17:37
  • "Also, what's $('#...').each supposed to do? There is hopefully only one ID per document." Pasting error, this is supposed to be a class, not id :-) I will change it accordingly. – stavarotti Jul 15 '11 at 17:40

1 Answers1

0

Try this

$( '#elm1' ).each( function() {

 $.each($(this).data("events"), function(i, event) {
        alert(i);
        $.each(event, function(j, h) {
            alert(h.handler);
        });
 });
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • 1
    jQuery's .data() is specific to jQuery. It will not always work for not "bound using jQuery" events. – DefyGravity Jul 15 '11 at 17:40
  • Yes, you should use it only if you are using jQuery library. – ShankarSangoli Jul 15 '11 at 17:42
  • I was not aware that .data() was specific to events created/attached via the jQuery object. Thanks for the quick response. I will use regular javascript to fish this out instead. – stavarotti Jul 15 '11 at 17:46
  • You question it self shows you are using jQuery then why do you want to use plain javascript and worry about cross browser issues? – ShankarSangoli Jul 15 '11 at 17:48
  • .data is not event specific, it stores arbitrary data associated with the specified element. [http://api.jquery.com/jQuery.data/] – ShankarSangoli Jul 15 '11 at 17:49
  • The events are attached by method(s) in javascript files that I can not edit - more specifically, the files are controlled by an existing framework. I want(ed) to utilize jQuery to augment existing functionality. – stavarotti Jul 15 '11 at 17:53
  • This solution only works for "jQuery events", not javascript events in general. – bukzor Jan 22 '13 at 20:15
  • It's not clear; even developers which are "using the jQuery library" will not see all events using this code. Any native events will not be seen. – bukzor Jan 22 '13 at 23:13