Advanced object selection using Mootools

Mootools’ “Selectors” utility has some really strong feature. It really saves a lot of time and sweat. Using this property, one can select a tag (any one can!), tag with specific name, id or any other attribute (huh!) and most amazingly part of a property value matching to some string…

Say you want to select all the div tags which has ID starting with ‘my’. How can you do that? Select all the div in the document, parse everyone’s id? Well, here this is a one line code in Mootools using the getElements function. It actually uses the general RegEx symbols to match property values.

To select the div with id starting with ‘my’ it would use getElements(‘div[id^=my]’);

Some other examples from Mootools documentation:

//Returns all anchors within myElement.
$('myElement').getElements('a');
//Returns all input tags with name "dialog".
$('myElement').getElements('input[name=dialog]');
//Returns all input tags with names ending with 'log'.
$('myElement').getElements('input[name$=log]');
//Returns all email links (starting with "mailto:").
$('myElement').getElements('a[href^=mailto:]');
//Adds events to all Elements with the class name 'email'.
$(document.body).getElements('a.email').addEvents({
    'mouseenter': function(){
        this.href = '[email protected]';
    },
    'mouseleave': function(){
        this.href = '#';
    }
});

Mootools documentation on Selectors utility is here: http://mootools.net/docs/core/Utilities/Selectors

One thought on “Advanced object selection using Mootools

  1. I just have to say this was probably one of the most effective posts I have read on the topic so far. I do not have any idea where you learn all your data but keep it coming! I am going to send some people on over to check this out. Amazing, simply awesome. I’m have just started getting into spitting out articles myself, nothing close to your writing skills (ha!) but I’d love for you to look over my article someday! bowflex

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.