Implementing jQuery-Like Event Syntax in MooTools

By  on  

If you take a peek inside the MooTools core Element/Events source code, you'll see the following collection of events:

Element.NativeEvents = {
	click: 2, dblclick: 2, mouseup: 2, mousedown: 2, contextmenu: 2, //mouse buttons
	mousewheel: 2, DOMMouseScroll: 2, //mouse wheel
	mouseover: 2, mouseout: 2, mousemove: 2, selectstart: 2, selectend: 2, //mouse movement
	keydown: 2, keypress: 2, keyup: 2, //keyboard
	focus: 2, blur: 2, change: 2, reset: 2, select: 2, submit: 2, //form elements
	load: 1, unload: 1, beforeunload: 2, resize: 1, move: 1, DOMContentLoaded: 1, readystatechange: 1, //window
	error: 1, abort: 1, scroll: 1 //misc
};

As you probably know, in order to add an event, you need to code something similar to:

	$('element').addEvent('click',function(e) {
		//do stuff
	});

In jQuery you code something like:

	$('#element').click(function(e) {
		//do stuff
	});

I don't prefer that syntax but if you're a Moo developer and you wanted to implement jQuery-like syntax for all events or you're slowly making your way from jQuery to MooTools, you can use the event collection cited above and the snippet to follow to quickly make that possible.

//hash the element.natives so we can do stuff with it
var hash = new Hash(Element.NativeEvents);
//remove items that need to be replaced, add their replacements
hash.erase('mouseover').erase('mouseout').erase('DOMMouseScroll');
hash.include('mouseenter',1).include('mouseleave',1);
//initialize this
var eventHash = new Hash({});
//for every event type, add to our hash
hash.getKeys().each(function(event){
	eventHash[event] = function(fn) {
		this.addEvent(event,fn);
		return this;
	}
});
//make it happen
Element.implement(eventHash);

Here's a few examples of using the newly implemented methods:

/* examples */
window.addEvent('domready',function() {
	$$('a').click(function(e) {
		e.stop();
		alert('mouse click');	
	});
	$$('a').contextmenu(function(e) {
		e.stop();
		alert('right click');	
	});
	$('myInput').keypress(function(e) {
		alert('key pressed: ' + e.key);
	});
});

Like I said, I don't find this syntax appealing, but I wanted to show how easy it is to do.

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
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

Incredible Demos

  • By
    FileReader API

    As broadband speed continues to get faster, the web continues to be more media-centric.  Sometimes that can be good (Netflix, other streaming services), sometimes that can be bad (wanting to read a news article but it has an accompanying useless video with it).  And every social service does...

  • By
    Create a Context Menu with Dojo and Dijit

    Context menus, used in the right type of web application, can be invaluable.  They provide shortcut methods to different functionality within the application and, with just a right click, they are readily available.  Dojo's Dijit frameworks provides an easy way to create stylish, flexible context...

Discussion

  1. I think that the sintax clear and user friendly of mootools is one of the best things for ppl in doubt between JQuery and MooTools. I liked a lot the article you posted about a page with both the two framework… but this way of write event in a jquery way in mootools is something that who uses mootools will not use, and who uses jquery can’t see how easy is write and speak in MooTools syntax… You and your blog you are a reference point for many of us, is not a criticism.
    see you
    Moonkiki

  2. I don’t like jQuery’s event syntax at all. In fact, I think it’s ugly. But someone trying to move from jQuery to MooTools could benefit from this.

  3. yes, you’re right. Little step from ugly to pure code.

    Yesterday i read in some links from your twitter to other internet’s beach, the Einstein phrase: IF YOU CAN’T EXPLAIN IT SIMPLY, YOU DON’T UNDERSTAND IT ENOUGH…. i thougth suddenly to MooTools and Jquery syntax :)

  4. Nice little jQuery to Mootools trick!

  5. @david: great job… but this behavior couldn’t be used only with the “migrators”, but this could at least clear the views a little bit :)

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