





















































In order to successfully and effortlessly write unobtrusive JavaScript, we must have a way to point to the Document Object Model (DOM) elements that we want to manipulate. The DOM is a representation of objects in our HTML and a way to access and manipulate them. In traditional JavaScript, this involves a lot (like, seriously a lot) of code authoring, and in many instances, a lot of head-banging-against-wall-and-pulling-out-hair as you discover a browser quirk that you must solve.
Let me save you some bumps, bruises, and hair by showing you how to select DOM elements the MooTools way. This article will cover how you can utilize MooTools to select/match simple (like, "All div elements") up to the most complex and specific elements (like, "All links that are children of a span that has a class of awesomeLink and points to http://superAwesomeSite.com
MooTools selects an element (or a set of elements) in the DOM using CSS selectors syntax.
Just a quick refresher on CSS terminology, a CSS style rule consists of:
selector {
property: property value;
}
For example, if you wanted to select a paragraph element with an ID of awesomeParagraph to give it a red color (in hexadecimal, this is #ff0), in CSS syntax you'd write:
#awesomeParagraph {
color: #ff0;
}
Also, if I wanted to increase its specificity and make sure that only paragraph elements that have an ID of awesomeParagraph is selected:
p#awesomeParagraph {
color: #ff0;
}
You'll be happy to know that this same syntax ports over to MooTools. What's more is that you'll be able to take advantage of all of CSS3's more complex selection syntax because even though CSS3 isn't supported by all browsers yet, MooTools supports them already. So you don't have to learn another syntax for writing selectors, you can use your existing knowledge of CSS - awesome, isn't it?
The $() and $$() functions are the bread and butter of MooTools. When you're working with unobtrusive JavaScript, you need to specify which elements you want to operate on.
The dollar and dollars functions help you do just that - they will allow you to specify what elements you want to work on.
Notice, the dollar sign $ is shaped like an S, which we can interpret to mean 'select'.
The dollar function is used for getting an element by it's ID, which returns a single element that is extended with MooTools Element methods or null if nothing matches. Let's go back to awesomeParagraph in the earlier example. If I wanted to select awesomeParagraph, this is what I would write:
$('awesomeParagraph’)
By doing so, we can now operate on it by passing methods to the selection. For example, if you wanted to change its style to have a color of red. You can use the .setStyle() method which allows you to specify a CSS property and its matching property value, like so
$('awesomeParagraph').setStyle('color', '#ff0');
The $$() function is the $() big brother (that's why it gets an extra dollar sign). The $$() function can do more robust and complex selections, can select a group, or groups of element’s and always returns an array object, with or without selected elements in it.
Likewise, it can be interchanged with the dollar function. If we wanted to select awesomeParagraph using the dollars function, we would write:
$$('#awesomeParagraph')
Note that you have to use the pound sign (#) in this case as if you were using CSS selectors.
If you need to select just one element that has an ID, you should use the $() function because it performs better in terms of speed than the $$() function.
Use the $$() function to select multiple elements. In fact, when in doubt, use the$$() function because it can do what the $() function can do (and more).
A note on single quotes (') versus double quotes ("")
The example above would work even if we used double quotes such as $("awesomeParagraph") or $$("#awesomeParagraph") ") - but many MooTools developers prefer single quotes so they don’t have to escape characters as much (since the double quote is often used in HTML, you'll have to do "in order not to prematurely end your strings). It's highly recommended that you use single quotes - but hey, it's your life!
Now, let's see these functions in action. Let's start with the HTML markup first. Put the following block of code in an HTML document:
<body>
<ul id="superList">
<li>List item</li>
<li><a href="#">List item link</a></li>
<li id="listItem">List item with an ID.</li>
<li class="lastItem">Last list item.</li>
</ul>
</body>
What we have here is an unordered list. We'll use it to explore the dollar and dollars function. If you view this in your web browser, you should see something like this:
Let's select the list item with an ID of listItem and then give it a red color using the .setStyle() MooTools method.
$('listItem').setStyle('color', 'red');
$('superList').setStyle('border', '1px solid #000000');
We'll be using the same HTML document, but this time, let's explore the dollars function.
alert( $$('.lastItem').getFirst().get('text') );
What if we wanted to select multiple sets of elements and run the same method (or methods) on them? Let's do that now.
$$('li a, .lastItem').tween('margin-left', '50px');
We just explored the dollar and dollars function to see how to select different elements and apply methods on them. You just learned to select:
You also saw how you can execute methods on your selected elements. In the example, we used the .setStyle(), .getFirst(), get(), and .tween() MooTools methods.
Because DOM selection is imperative to writing MooTools code, before we go any further, let's talk about some important points to note about what we just did.