Implement MooTools’ Elements.addEvent in jQuery

By  on  

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.

The Usage

$(document).ready(function() {
	$('#myElement').addEvent('click',function(){ alert('w00t'); });
});

Looks a lot like MooTools, no? If you like jQuery's syntax of .click() type events, read my post: Implementing jQuery-Like Event Syntax in MooTools

Recent Features

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    MooTools Star Ratings with MooStarRating

    I've said it over and over but I'll say it again:  JavaScript's main role in web applications is to enhance otherwise boring, static functionality provided by the browser.  One perfect example of this is the Javascript/AJAX-powered star rating systems that have become popular over the...

  • By
    Flexbox Equal Height Columns

    Flexbox was supposed to be the pot of gold at the long, long rainbow of insufficient CSS layout techniques.  And the only disappointment I've experienced with flexbox is that browser vendors took so long to implement it.  I can't also claim to have pushed flexbox's limits, but...

Discussion

  1. More concise :

    jQuery.fn.addEvent = jQuery.fn.bind;

  2. Well put Pierre…well put.

  3. I too prefer verbs like el.setStyle v. el.css. But swapping syntax would totally goof me up!

  4. I don’t get it, why not just use

    $('#myelement').bind('click', function() {...});
    

    It’s even shorter than ‘addEvent’

  5. I agree with Corey, $().bind is exactly what you’re looking for…

  6. Bo Hunter

    I would have to agree, this is nothing more than an alias.

  7. Mysteriously enough in jQuery version 1.4 I get this:

    $(“#website_stock”).addEvent is not a function

    When I set it as just click(fn) it works in Firefox, but not in IE8. It doesn’t return any errors – just the functions do NOT run whatsoever. Same with the bind() option – works in FF on mac, not on IE8.

  8. Pradeep

    Hi David,
    In this site, the two sides with name older and newer and toggle in-out,how to implement it please send me the complete demo with code if possible.
    Thanks in advance

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!