View Bound Events from an Element in jQuery

The .data() method is a neat way to find out what data is associated with an element. For example, let’s say we have the following HTML:

<a href="http://www.google.com" id="google-link">Click me to proceed.</a>

and we run the following:

$("#google-link").data("events");

We’ll get (in Chrome’s console):

We get an undefined because there are no events bound to it. Now, let’s add some events:

    $("#google-link").click(function(e) {      
      alert("Google link was clicked.");
      e.preventDefault();
    });
 
    $("#google-link").hover(function(e) {      
      alert("Google link was hovered.");
      e.preventDefault();
    });

Now let’s view the page and run the data method again in the console.

$("#google-link").data("events");

The results:

Thus it’s very handy, especially when you’re using plugins and they misbehave by randomly hooking themselves into elements, getting in your way.

Leave a Reply