Implement jQuery-like “Click” Syntax In MooTools 1.2
I've updated this post's content per reader comments. Thanks to everyone for their suggestions!
While I prefer the MooTools syntax over the jQuery syntax, I do respect the jQuery syntax. Here's a quick way to make your MooTools look a little like jQuery by implementing the Element.click() function.
The MooTools JavaScript
Element.implement({
'click': function(fn) {
return this.addEvent('click',fn);
}
});
The MooTools JavaScript Usage
window.addEvent('domready',function() {
//show an alert when the element is clicked.
$('click-me').click(function() {
alert('Clicked');
});
});
This syntax will allow for shorter code if you do a lot of click event programming.
![How I Stopped WordPress Comment Spam]()
I love almost every part of being a tech blogger: learning, preaching, bantering, researching. The one part about blogging that I absolutely loathe: dealing with SPAM comments. For the past two years, my blog has registered 8,000+ SPAM comments per day. PER DAY. Bloating my database...
![9 Mind-Blowing WebGL Demos]()
As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us. Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos. Another technology available...
![LightFace: Facebook Lightbox for MooTools]()
One of the web components I've always loved has been Facebook's modal dialog. This "lightbox" isn't like others: no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much." With Facebook's dialog in mind, I've created LightFace: a Facebook lightbox...
![CSS Sprites]()
The idea of CSS sprites is pretty genius. For those of you who don't know the idea of a sprite, a sprite is basically multiple graphics compiled into one image. The advantages of using sprites are:
Fewer images for the browser to download, which means...
Hey, Just wanted to say thanks for your blog, its really helped me along with Mootools, and I enjoy working and creating things with ease.
I just need to know thou,
Should I be switching over to JQuery.
with Microsoft’s decision to incorporate JQuery, I’m wondering if I’m using the wrong framework.
I read the article at juliocapote.com
and well I just need a Yes or a No.
I trust your judgment.
and
Iis the future of Mootools sound?
Nice, but why not just pass the whole ‘fn’ to the ‘addEvent’ function? Eg:
.. because some coders might probably do this:
… and probably some don’t want to ‘stop’ the event by default.
@Lim Chee Aun: Great tip! I was trying to get something similar to work but couldn’t get it going. Thanks for sharing!
@david: No problem. I’m actually working (very slowly) on porting (almost) all jQuery-style syntax to Mootools. Not sure if it will be useful though.
Something I’ve thought of that might be a slight problem, though haven’t tested, is binding.
I could see someone trying to use this keyword, and might even remember to bind it in the click function, but when it gets used in the addEvent part, you didnt bind this.
Like I said, haven’t tested for actual failure, but in theory it sounds breakable :D
In my meager JavaScript dabblings, I’ve always been jealous of jQuery’s event syntax. Needless to say, I’m really happy to see this as an option.
I’m somewhat of a hobbyist, so I apologize in advance for any mistakes. I did a little playing around and it looks like it doesn’t break .bind(this). This, of course, is a trivial implementation and there’s a chance that I totally misused .bind()
If anyone’s curious, the link in my name goes to my uh … ultra-complex tests :P
@david
i think you should “return this”
after implementation