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 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...

  • 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?

Incredible Demos

  • By
    MooTools Image Preloading with Progress Bar

    The idea of image preloading has been around since the dawn of the internet. When we didn't have all the fancy stuff we use now, we were forced to use ugly mouseover images to show dynamism. I don't think you were declared an official...

  • By
    Six Degrees of Kevin Bacon Using MooTools 1.2

    As you can probably tell, I try to mix some fun in with my MooTools madness but I also try to make my examples as practical as possible. Well...this may not be one of those times. I love movies and useless movie trivia so naturally I'm...

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!