Thursday, March 3, 2011

How do I learn if events are bound to an element using jquery?

I need a hasEvents() method like

var someBool = hasEvents($("#myelement"));

that returns true if there are some bound events to any of the element's event handlers.

From stackoverflow
  • have you taken a look at the hasEvent() plugin? the code is pretty small:

    (function(A) {
        A.fn.hasEvent = function(C) {
           var B = this.data("events");
           return( B && B[C] )
        }
    }) (jQuery)
    

    for your specific purpose you could modify it slightly:

    (function($) {
        $.fn.hasEvents = function() {
            return new Boolean(this.data('events') );
        }
    }) (jQuery);
    
    $('#someDiv').click(function() {
        alert('new event');
    });
    
    $('#someDiv').hasEvents();      // true
    $('#someOtherDiv').hasEvents(); // false
    

0 comments:

Post a Comment