How well do you know Mootools?

After reading the Prototype checklist, I thought I’d take all those examples and apply them to Mootools. So here we go (sorry, mine won’t be styled as nicely):

One

The wrong way

document.getElementById('foo');

The right way

$('foo');

This is the standard way of getting elements, started by Prototype. This will probably be the same for all popular libraries.

Two

The wrong way

var woot = document.getElementById('bar').value;
// or
var woot = $('bar').value;

The right way

var woot = $('bar').getValue();

Mootools doesn’t have a shortcut for getting form values, but it does have a chainable method.

Three

The wrong way

$('footer').style.height = '100px';
$('footer').style.background = '#ffc';

The right way

$('footer').setStyles({
  height: '100px',
  background: '#ffc'
});

Almost the same as Prototype, but don’t forget the ’s’ on setStyles.

Four

The wrong way

$('coolestWidgetEver').innerHTML = 'some nifty content';

The right way

$('coolestWidgetEver').setHTML('some nifty content');

Same as Prototype, but ’setHTML’ instead of ‘update’ (’setHTML’ makes more sense imo).

Five

The wrong way

new Ajax('ninja.php?weapon1=foo&weapon2=bar');

The right way

new Ajax('ninja.php', {
  data: {
    weapon1: 'foo',
    weapon2: 'bar'
  }
});

If the values are static, I’d prefer to use the first example, really. But kangax argues that the second example is “cleaner and better structured parameters definition.”

Six

The wrong way

new Ajax('blah.php', {
  method: 'post',
  async: true,
  encoding: 'UTF-8',
  headers: {'contentType': 'application/x-www-form-urlencoded'}
});

The right way

new Ajax('blah.php');

These options are all defaults, no need to set them.

Seven

The wrong way

$('myContainer').onclick = doSomeMagic;

The right way

$('myContainer').addEvent('click', doSomeMagic);

I changed the wrong way example because Prototype has a non-object-oriented way of attaching events that doesn’t apply to Mootools. Anyway, Mootools implements its own cross-browser event methods, so you should use them instead.

Eight

The wrong way

$$('div.hidden').each(function(el){
  el.removeClass('hidden');
})

The right way

$$('div.hidden').removeClass('hidden');

Slightly different examples, as Mootools doesn’t have the show() or invoke() methods. Anyway, Mootools can automatically apply Element methods to a collection of elements. No need to iterate through them.

Nine

The wrong way

$$('div.collapsed').each(function(el){
  el.addEvent('click', expand);
});

The right way

$$('div.collapsed').addEvent('click', expand);

Again, you don’t have to iterate over a collection of elements if you want to call an Element method on all of them.

Ten

The wrong way

$$('input.date').addEvent('focus', onFocus);
$$('input.date').addEvent('blur', onBlur);

The right way

$$('input.date').addEvents({
  'focus': onFocus,
  'blur': onBlur
});

The point of this example for Prototype was supposed to be chaining, but Mootools provides a function to add multiple events, which looks a lot nicer.

Eleven

The wrong way

$('productTable').innerHTML = 
  $('productTable').innerHTML + 
  '<tr><td>' + productId + ' '
  + productName + '</td></tr><tr><td>' 
  + productId + ' ' + productPrice + 
  '</td></tr>';

The right way

var template = '<tr><td>#{id} #{name}</td></tr><tr><td>#{id} #{price}</td></tr>';
$('productTable').injectHTML(template.interpolate({
  id: productId,
  name: productName,
  price: productPrice 
}));

Note that this Mootools functionality doesn’t come out-of-the-box. You’ll need to grab this string interpolation plugin and injectHTML.

That’s It

Just so we’re on the same page, these examples are done with Mootools v1.11.

There is a part 2 of the original article but it seems pretty specific to Prototype. Maybe I’ll come up with more shortcuts in another article for Mootools.

18 Responses to “How well do you know Mootools?”

  1. Michelle

    Awesome article, Chris!

    Puts the people who think MooTools is just for the fancy effects in their place. :)

  2. Newsman

    [Quote]Mootools doesn’t have a shortcut for getting form values, but it does have a chainable method.[/Quote]

    What about the method explained in http://clientside.cnet.com/wiki/mootorial/03-native/00-element-extensions under “Element.Form.js” section???

  3. Chris

    Newsman, I meant it doesn’t have a shortcut like Prototype’s $F() function. In my example, I do indeed use Mootools’s getValue() method explained in the article you linked.

  4. Laurentiu Iovan

    Hi Chris,

    Awesome article, keep up the good work and thanks.

  5. ongray

    thanks for the good article.
    I can see clearly now, the cloud is gone.
    I have been using Mootools mixing the right and wrong way and have been wondering what is the different. You have given the answer, the wrong way and the right way.

  6. jc

    thanks for the good advice found here. I recommend you check this site on IE 7. The background is a little off.

  7. carmit

    hi
    tnx for the advise and knowledge.
    i like pluralism so…
    there is no a one only right way and other wrong ways
    there is only ways and it is good and nice to know them all and accept them all.
    The way you call “right way” is just object oriented techniqe… and i also use it
    but dont underestimate the other ways and dont give them the WRONG TAG.

    tnx alot.

  8. Edkar-Matienzo

    This brings me to an idea:…

  9. Rob Snow

    I wonder if I can ask.
    I am using a javascript based carousel image gallery and a text scroller. Both reside in different hidden divs.
    The issue is that when I initiate the div the javascript (mootool/jquery) element doesn’t show or work.
    Is there a way around this issue?
    I am not a javascript programmer at all!!!

    Thanks

  10. Алексей

    I already use on your website Mootools

  11. Phil Freo

    Your #11 links are out of date. Can you update / provide the best current solution?

    Thanks!

  12. Uhtu

    Спасибо за блог, очень грамотно все сделано.

  1. dashda » Blog Archiv » How well do you know Mootools?
    Pingback on November 10th, 2007 at 9:59 am
  2. chromaSYNTHETIC Journal » Blog Archive » Mootools Short-cuts Part II
    Pingback on November 13th, 2007 at 6:48 pm
  3. Learning MooTools: MooTools Tutorials and Examples - Six Revisions
    Pingback on June 19th, 2008 at 7:43 pm
  4. 20 MooTools Tutorials and Examples | Mind Tree
    Pingback on August 4th, 2008 at 11:35 pm
  5. Learning MooTools: 20 MooTools Tutorials and Examples | [w3b]ndesign
    Pingback on August 17th, 2008 at 2:19 pm
  6. Webiest : Links for January 27th
    Pingback on January 27th, 2009 at 12:00 pm

Leave a Reply