Mootools hasEvent

Wednesday, 18 June 2008

If you have ever dealt with events in JavaScript then you know that it can often be a cumbersome task. The Mootools library makes it dead simple to add or remove events to any given element. The problem I discovered when working with Mootools and events was checking to see whether a given element has an event. Natively Mootools does not come with a prepackaged method to check elements for the existance of any event. I use the following snippet of code to extend the Mootools library to have a hasEvent method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Native.implement([Element, Window, Document], {

    hasEvent: function(type) {
        var events = this.retrieve('events', {});

        if (events && events[type]) { return true; }

        else { return false; }
    }
});

Leave a comment