Check if event exists on element [duplicate]

Is there a way to check if an event exists in jQuery? I’m working on a plugin that uses custom namespaced events, and would like to be able to check if the event is binded to an element or not.

$('body').click(function(){ alert('test' )})

var foo = $.data( $('body').get(0), 'events' ).click
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.

$.each( foo, function(i,o) {
    alert(i) // guid of the event
    alert(o) // the function definition of the event handler

You can inspect by feeding the object reference ( not the jQuery object though ) to $.data, and for the second argument feed 'events' and that will return an object populated with all the events such as 'click'. You can loop through that object and see what the event handler does.

You may use:


to make sure all click events are unbinded, then attach your event

To check for events on an element:

var events = $._data(element, "events")

Note that this will only work with direct event handlers, if you are using $(document).on("event-name", "jq-selector", function() { //logic }), you will want to see the getEvents function at the bottom of this answer

For example:

 var events = $._data(document.getElementById("myElemId"), "events")


 var events = $._data($("#myElemId")[0], "events")

Full Example:

        <script src="//" type="text/javascript"></script>
            $(function() {
                $("#textDiv").click(function() {
                    //Event Handling
                var events = $._data(document.getElementById('textDiv'), "events");
                var hasEvents = (events != null);
        <div id="textDiv">Text</div>

A more complete way to check, that includes dynamic listeners, installed with $(document).on

function getEvents(element) {
    var elemEvents = $._data(element, "events");
    var allDocEvnts = $._data(document, "events");
    for(var evntType in allDocEvnts) {
        if(allDocEvnts.hasOwnProperty(evntType)) {
            var evts = allDocEvnts[evntType];
            for(var i = 0; i < evts.length; i++) {
                if($(element).is(evts[i].selector)) {
                    if(elemEvents == null) {
                        elemEvents = {};
                    if(!elemEvents.hasOwnProperty(evntType)) {
                        elemEvents[evntType] = [];
    return elemEvents;

Example usage:


use jquery event filter

you can use it like this


I wrote a plugin called hasEventListener which exactly does that.

Hope this helps.

Below code will provide you with all the click events on given selector:


You can iterate over it using each or for ex. check the length for validation like:


Thought it would help someone. :)

This work for me it is showing the objects and type of event which has occurred.

    var foo = $._data( $('body').get(0), 'events' );
    $.each( foo, function(i,o) {
    console.log(i); // guide of the event
    console.log(o); // the function definition of the event handler

I ended up doing this

typeof ($('#mySelector').data('events').click) == "object"