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
Leave a Reply