Implement MooTools’ Elements.addEvent in jQuery

One thing that I’ve always disliked about the jQuery javascript framework is its syntax for adding event listeners to elements. The way to add a click event to an element using jQuery is:

$('#myElement').click(function() {
//do work
});

I love OOP so the above code tells me that a click is being triggered, not that it’s going to add a listener to the element. There’s nothing wrong with jQuery using that syntax; I simply don’t prefer it. What I do when I need to work with jQuery is Moo-ify its syntax when I can. Here’s how you can implement MooTools’ “addEvent” syntax in MooTools.

The jQuery Javascript

jQuery.fn.addEvent = jQuery.fn.bind; //updated

We add the “addEvent” function to the jQuery.fn object. Pretty simple.