Mootools element.getChildren() Vs element.getElements() – What’s the difference?
I have been writing a fair bit of Javascript recently and came up with a couple of situations where I needed to use Mootools JavaScript element.getChildren() instead of element.getElements() so I thought i’d share a little info which may or may not be obvious about these two very useful Mootools methods.
At first glance of the Mootools documentation, element.getChildren() and element.getElements() look pretty much identical, but what’s not obvious (to me at least) is the fundamental difference between the two methods:
element.getElements() recurses through all child elements from it’s initial starting point whereas element.getChildren() does not, it returns only direct children of the element from which it begins.
For example, if you had a fragment of HTML like this:
<ul id="list_one">
<li>Item one</li>
<li>Item two</li>
<li>
<ul>
<li>sub item one</li>
<li> sub item two</li>
</ul>
</li>
</ul>
you could then have some JavaScript like this to perform an operation on it:
window.addEvent("domready", function()
{
var list=$("list_one");
list.getChildren("li").each(function(el, i)
{
// do something
});
});
and that would affect only the 3 immediate children <li> of the outer <ul> and would not affect the “sub items” <li> in the inner <ul>.
NOTE: both element.getChildren() and element.getElements() return an array of DOM elements, hence looping around the result using each().
Conversely, if you wanted to affect all <li> within the outer <ul> you could do something like this:
window.addEvent("domready", function()
{
var list=$("list_one");
list.getElements("li").each(function(el, i)
{
// do something
});
});
There’s tons more element methods such as element.getFirst() which retrieves the first matching child element.
If you include selectors in your Mootools download, you can add css-style selectors to your methods e.g.:
window.addEvent("domready", function()
{
var list=$("list_one");
list.getElements("li.some_class").each(function(el, i)
{
// do something
});
});
which would affect only <li> which have a CSS class of “some_class”.
Hope that makes sense and saves someone some time!
| This entry was posted by Neil Craig on August 6, 2010 at 5:00 pm, and is filed under General Web-Development, Javascript, MooTools. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No trackbacks yet.
Mobile Website design & development guidelines
about 2 years ago - 1 comment
Like it or hate it, the Apple iPhone has ushered in the age of the “Smartphone” which in turn has delivered mobile web-browsing to the masses. The mobile browser, whilst now broadly quite capable (at least on leading platforms), has introduced a slightly different set of requirements in terms of design and development of Websites.
Many, many designers, developers and others have written articles on how to design and/or develop for the mobile browser. I am aiming to summarise what I have learnt from several sources alongside my experience an opinion in a (hopefully) relatively quick and simple way
Solution: Javascript returns incorrect width or height of an element containing image(s) when page first loads
about 2 years ago - 9 comments
Recently, I’ve been doing quite a lot of JavaScript development, part of which involved equalising heights of a group of similar elements on the page. I thought this would be very simple and indeed it was but it threw up a JavaScript quirk which I have seen several times before. The solution is simple, but More >
Mootools element.morph() example
about 2 years ago - 3 comments
A simple example of the time-saving element.morph() Mootools method which allows you to morph HTML elements from one property set to another.
CSS Basics: The difference between outline and border
about 2 years ago - 3 comments
CSS outline and border are at a glance very similar but have a few important differences: Outline is effectively overlaid over the top of the element it is applied to and therefore outline does not add to the element width whereas border does add to the element width Outline (according to the official W3C specification More >
A simple example of CSS sprites
about 2 years ago - 7 comments
CSS Sprites can help to reduce web page download times and delays caused by downloading of hover/rollover background images. This article walks through the basics with an example of how you can use CSS sprites in a graphical HTML menu.
A simple example of navigator.geolocation.watchPosition()
about 3 years ago - 20 comments
As I mentioned in my previous post about the new/upcoming navigator.geolocation standard, I have been experimenting with Geo location on my IPhone so I thought I’d share some of that with you in case it helps anyone out. My example is very simple and is intended to show how you can use the JavaScript navigator.geolocation.watchPosition() More >
Multibox – a Multimedia alternative to Lightbox/Slimbox for Mootools 1.2
about 3 years ago - 1 comment
Since the original Lightbox was released by Lokesh Dhakar many moons ago, we’ve all been fascinated with it’s JavaScript trickery. Those of us who are Web-Developers working with/for Design Agencies will no doubt be very familiar with many subsequent, similar JavaScripts such as Slimbox. I’ve tried out a fair number of MooTools compatible Lightbox clones/improvements More >
JSH5F – A Javascript method to enable HTML5 Form Elements in Browsers which support them
about 3 years ago - 2 comments
HTML 5 offers 13 new form input element types but Browser support for these new input types is very scant at present. However, with JSH5F, you can use the new HTML 5 form elements right now in supporting Browsers without causing problems for people using non-supporting browsers.
UPDATE 2: HTML 5 Offline Application Caching (Javascript localStorage/sessionStorage) – A very simple, reasonably complete example
about 3 years ago - 3 comments
In an effort to stay ahead or at least alongside the game, I am starting to investigate the upcoming technologies of HTML5 and associated Javascript. In this article, i’ll cover a simple example of Javascript localStorage/sessionStorage, how to detect on-line/off-line status and off-line cache Manifest files.
W3C compliant Adobe Flash embedding/insertion
about 3 years ago - 5 comments
Adobe Flash is still very popular despite the massive improvements in Javascript animation over recent years. However, some important devices such as the Apple IPhone lack the capability to display Flash movies. Because of this and due to my immutable desire to produce W3C compliant HTML, I’ve recently had to think about the method I use to embed Adobe Flash movies in a web page.
This article is a short review of the two methods I consider best.

about 2 years ago
Great site. A lot of useful information here. I’m sending it to some friends!