1

Possible Duplicates:
How to debug Javascript/jQuery event bindings with FireBug (or similar tool)
How to find event listeners on a DOM node?

How do I detect which events have been bound to a DOM element through JavaScript? In the example below I would like to be able to see that the click event has been bound on my 'clickable' span, and if possible be able to debug from the alert-line.

Can I do this with a web developer utility like Firebug?

<html>
    <head>
         <title>Events bound with JavaScript</title>
         <script type="text/javascript"
             src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
         <script type="text/javascript">
             $(document).ready(function() {
                 $("#clickable").click(function() {
                     alert("Clicked.");
                 });
             });
         </script>
    </head>
    <body>
         <span id="clickable">Click me</span>
    </body>
</html>
2

1 Answer 1

1
// this function will return number of keys in object
function objectLenght (obj) {
    var len = 0;
    for (var key in obj) {
        len++;
    }
    return len;
}

var objWithClickEvents = [];

// iterate through all elements
$('*').each(function() {
    var clickEvents = $.data($(this)[0], 'events').click;
    if (objectLength(clickEvents) > 0) {
        objWithClickEvents[objWithClickEvents.length] = $(this);
    }
});

Replace click with other event if you wish.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.