Prototype’s Element.on with MooTools

By  on  

Prototype 1.7RC1 was released on Monday. While the big news with this release appears to be the implementation of John Resig's Sizzle selector engine, what caught my eye was the new on method. This new Element.on method is very similar to Prototype's Element.observe method but with a few enhancements (event relaying, event stop). While I don't necessarily care about the enhancements, I simply love that the event method is called "on." Using "on" as the method name makes it sound more like the event itself. Here's how easy it is to use "on" instead of "addEvent" for your events.

The MooTools JavaScript

/* the directive */
Element.alias('addEvent','on');

/* the usage */
$('myElement').on('click',fn);

Using on instead of addEvent is as easy as a quick MooTools Element.alias directive.

It's weird that I like "on" so much considering a generally don't like jQuery's css and attr methods. Oh well. Congrats to the Prototype team for their impending release.

Recent Features

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    Instagram For MooTools

    If you're still rocking an iPhone and fancy taking a photo every now and then, you'd be crazy not to be using an app called Instagram.  With Instagram you take the photos just as you would with your native iPhone camera app, but Instagram...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Discussion

  1. Lorenzo S.

    Hi David. Nice idea. I like also css() and other jQuery methods’ name (for their simplicity), but I prefer Mootools core. So, why not:

    Element.alias('addEvent','on'); 
    Element.implement({
      'css': function(p, v) {
        if (v == undefined) return this.getStyle(p);
        else return this.setStyle(p, v);
      },
      'attr': function(a, v) {
        if (v == undefined) return this.getProperty(a);
        else return this.setProperty(a, v);
      },
      'html': function(h) {
        if (h == undefined) return this.get('html');
        else return this.set('html', h);
    });
    

    What do you think?

  2. @Lorenzo S.: I’ve done posts like that in the past. I can’t stand .css and .attr for MooTools, but what you have would work.

  3. Arian

    @Lorenzo S.: If you want jQuery syntax in MooTools, have a look at this repository: http://github.com/cheeaun/mooj/blob/master/mooj.js

  4. Nikolaj

    @David

    Fully agree with “on” seems acceptable, whereas .css, .attr etc. doesn’t :)

    I guess the reason you might like “on” – when not other jQuery shortcuts – might be due to we’re used to “on” in “onClick” etc. for regular JavaScript, which probably makes the mind having positive thoughts.

  5. Great advice, thanks a lot.

    However, how much slower will this be? I mean, will on() trigger addEvent() and that in turn will trigger whatever internal functions are used?

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