Prototype’s Element.on with MooTools
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.
![CSS Filters]()
CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...
![Responsive and Infinitely Scalable JS Animations]()
Back in late 2012 it was not easy to find open source projects using requestAnimationFrame()
- this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...
![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?
![The Simple Intro to SVG Animation]()
This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery.
An SVG element is a special type of DOM element...
Hi David. Nice idea. I like also css() and other jQuery methods’ name (for their simplicity), but I prefer Mootools core. So, why not:
What do you think?
@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.
@Lorenzo S.: If you want jQuery syntax in MooTools, have a look at this repository: http://github.com/cheeaun/mooj/blob/master/mooj.js
@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.
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?