This post is the first in a series about getting started with MooTools.
The MooTools website states – “MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. It allows you to write powerful, flexible, and cross-browser code with its elegant, well documented, and coherent API.”
If you are just getting started with MooTools, the syntax is quite a bit more streamlined than stock JavaScript. I wanted to provide some examples of stock JavaScript and the MooTools equivalent. In general its easier to start learning by example then once you get some of the core ideas down you can be more successful as you continue to learn MooTools. Future parts in this series will focus on the individual concepts in more details, but for now the point is to get you writing streamlined MooTools code.
Note: The MooTools docs are an excellent source for any questions you have since there is tons of great info and examples there as well.
Core and More
MooTools is split into 2 files, Core and More. The Core library is the main file for MooTools that you will always need. The More library contains additional plugins that might be useful to you. There is a More builder that allows you pick and choose what optional components you need. This way you can keep the More size down to only the extra things you need. For now, I’ll just be dealing with concepts in the Core library.
Element references
JavaScript
//single reference
var el = document.getElementById('id');
//all div tags
var divs = document.getElementsByTagName('div');
MooTools
//single reference
var el = $('id');
var el2 = document.id('id'); //use this instead of $() with jQuery
//all tags
var divs = $$('div');
$() is a quick way to get an element reference and extend it with MooTools methods. Keep in mind you should use document.id() instead of you plan to use jQuery too (I don’t recommend mixing frameworks, but somtimes its unavoidable). $$() is a very powerful function that selects and extends a set of elements you specify. It returns an array and allows you to manipulate each item individually and even all at once.
Changing CSS properties
JavaScript
document.getElementById('id').style.backgroundColor = '#f00';
MooTools
//single css property
$('id').setStyle('background-color', '#f00');
//multiple css properties
$('id').setStyles({
'background-color': '#f00',
'color': '#000'
});
Setting styles is much more organized and allows you to stick with the “real” css property names when you are setting them. There are also getStyle() and getStyles() methods that work in a similar way but simply give you the values.
Manipulate innerHTML
JavaScript
document.getElementById('id').innerHTML = '<div><h1>header</h1></div>';
MooTools
//set innerHTML
$('id').set('html', '<div><h1>header</h1></div>');
//get innerHTML
$('id').get('html');
The set() and get() methods can be used with any attributes on the element. You can also pass in an object to the set() method to set multiple things in a single call.
CSS class manipulation
JavaScript
document.getElementById('id').className = 'class1 class2';
MooTools
//add class
$('id').addClass('class1');
$('id').addClass('class2');
//removes class (leaves other classes intact)
$('id').removeClass('class1');
//toggle class (removes if already applied, adds otherwise)
$('id').toggleClass('class2');
Class manipulation is much easier to deal with and the toggleClass() method is pretty helpful when used on click events to toggle something between 2 states.
Events
JavaScript
document.getElementById('id').onclick = function() {
alert('clicked!');
}
document.getElementById('id').onmouseover = function() {
this.style.cursor = 'pointer';
}
MooTools
//single event
$('id').addEvent('click', function() {
alert('clicked!');
});
//multiple events
$('id').addEvents({
'click': function() {
alert('clicked!');
},
'mouseover': function() {
this.setStyle('cursor', 'pointer');
}
});
Events are organized much better allows the code to be more concise.
Looping
JavaScript
var divs = document.getElementsByTagName('div');
//process each div individually
for(var i = 0; i < divs.length; i++) {
divs[i].style.backgroundColor = '#f00';
divs[i].onclick = function() {
alert('clicked!');
};
}
MooTools
var divs = $$('div');
//process each div individually
divs.each(function(div, index) {
div.setStyle('background-color', '#f00');
div.addEvent('click', function() {
alert('clicked!');
});
});
//process all divs at once (very helpful!)
divs.setStyle('background-color', '#f00');
divs.addEvents('click', function() {
alert('clicked!');
});
Using the $$() returns an array of elements and MooTools allows you to process each element individually like traditional JavaScript or you can use shorthand and apply to all the elements in the array. This is super helpful and quick to code whenever you want to change CSS, add classes, add events and more to the entire collection of elements.
Body onload event
JavaScript
window.onload = function() {
alert('loaded!');
}
MooTools
//fires before onload
window.addEvent('domready', function() {
alert('domready!');
});
domready is a custom event that MooTools adds that allows code to run as soon as the DOM is ready to go. No need to wait until the load event fires which is a little later. Of course you can also add mulitple events to the window as well using the addEvents() method.
The future
I’ll write more posts in the future with more common examples like above and also plan to dive into more specific topics (i.e. $$(), event handling, etc) to give even more insight. Let me know what you think.

















As soon as I found this website I went on reddit to share some of the love with them.
Level of comfort have fat – some of those tiny fatty tissue lines to the thighs along with buttocks. Cellulite appear thanks to bad diets gives you a negative circulation with the blood which causes waste to retail outlet up within the fat tissue rather then being flushed out.
היי, אם אתם רוצים לקבל מידע בתחום דשא סינטטי ואיך יש לשים אותו ולהשקות אותו , היכנסו לאתר האינטרנט העוסק בתחום.
Search engine optimization wants a very good search engine optimizer plan. Simply one among these strategies can make a giant distinction in your sites place and company your website brings you.
There are some extremely excellent ideas here. Can not wait to put some of these into action. Its very heading to bring very good vibrations where the vibrations should be
I can see that you are putting a lots of efforts into your blog. Keep posting the good work.Some really helpful information in there. Bookmarked. Nice to see your site. Thanks!
Okay article. I just became aware of your blog and desired to say I have really enjoyed reading your opinions. Any way I’ll be subscribing in your feed and Lets hope you post again soon.
keep up the fantastic piece of work, I read few posts on this site and I believe that your weblog is very interesting and contains bands of wonderful info .
Perfect piece of work you have done, this web site is really cool with excellent information.
become a play create, they write performs and send them into movie producers and they can generate millions, or just write books and submit your plays, wishing that your book reaches someone famous in the entertainment industry
your link on my blog here http://www.conveyancingquotes.info/?page_id=9 ,I view something really interesting about your website so I saved to my bookmarks .
Interesting article. You have a good review on this subject and I will be subscribing to your RSS feed and will hope you shall post again soon on similar matters. But I am curious on what your sources for the post are? Thanks a lot
פורטל עם מאמרים לשימוש חופשי . מאגר המאמרים היחיד בישראל המאפשר לכותבי המאמרים ליצור פרסומות גוגל אדסנס אישיות במאמרים שהם כותבים וכך גם להרוויח שקלים בנוסף לקישורים.
I want to show my passion for your kindness giving support to men and women that actually need help with this particular matter. Your special dedication to getting the message along was incredibly significant and has permitted employees just like me to achieve their pursuits. Your own helpful tutorial denotes a lot to me and further more to my peers. With thanks; from all of us.
This is my very first time i pay a visit to right here. I discovered so quite a few entertaining stuff inside your blog, in particular it’s discussion. From the many comments on your posts, I’m assuming I’m not the just one having all of the leisure right here! Sustain the superb get the job done.
After the past few days I’ve come to the realization that composing scripts is something I would really love to do. I’ve always jotted lower stories on paper and have always had ideas, but i’ve in no way written a piece of software in my life. I’m basically a beginner ,but where do I begin?
Heya, already been reading your blogging site for a long time. I manage a comparable blog page but I keep receiving a pile of spam comments, how can you maintain your blog page so clean?
original artwork
Excellent post however I was wanting to know if you could write a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Appreciate it!
Yeah you get it. This does assist. Try it out!
Normally I do not read post on blogs, but I wish to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, very nice post.
My brother recommended I would possibly like this website. He was once entirely right. This post truly made my day. You can’t consider simply how much time I had spent for this information! Thanks!
?YFYPpaideias? UK university bids to open New York campus BBC News – UK university bids to open New York campus? – bbc
Those designed for greatness are going to be tested and have to overcome obstacles while folks that are destined for mediocrity will have it effortless
Really? It really is excellent to witness anyone finally begin addressing this stuff, however I’m still not really certain how much I agree with you on it all. I subscribed to your rss feed though and will certainly keep following your writing and possibly down the road I may chime in once again in much more detail. Cheers for blogging though!
My brother suggested I may like this web site. He was entirely right. This submit truly made my day. You can not imagine just how a lot time I had spent for this information! Thank you!
kids art prints
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!
Well thought out article. You have a knowledgable opinion on this matter and I will be subscribing to your feed and hope you shall write again soon on similar matters. But I am would like to know what your article sources for the post are? Thanks
He that parts with his property before his death prepares himself for much suffering.
You ought to essentially think about working on developing this weblog into a significant authority in this market. You evidently have a grasp handle of the subjects everyone seems to be looking for on this web site anyways and you could possibly certainly even earn a buck or two off of some advertisements. I’d discover following current topics and elevating the quantity of write ups you place up and I assure you’d begin seeing some wonderful focused site visitors in the close to future. Just a thought, good luck in no matter you do!
Please let me know if you’re looking for a article writer for your blog. You have some really great articles and I feel I would be a good asset. If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link back to mine. Please blast me an email if interested. Cheers!coach outlet
I was unaware that this item was tucked away in the Ciao cafe, and when I found it, I thought that I would share with visitors a really important component of my life and how I make my living simply because all of the downfalls I have made in obtaining successful may help some would-be writer to turn out to be a published writer.
Nice read, I just passed this onto a friend who was doing a little research on that. And he actually bought me lunch as I found it for him smile Therefore let me rephrase that: Thank you for lunch!
Yay google is my king helped me to find this outstanding web site! .
your link on my blog here http://tinyurl.com/page-id-9 ,I like this post, enjoyed this one thankyou for putting up.
Pretty good article. You have a knowledgable review on this matter and I’ll be subscribing to your RSS and hope you will post frequently on similar subjects. But I was curious on what your article sources for the article are? Thanks a lot
Search engine optimization wants a superb search engine optimizer plan. Just one in every of these methods can make a giant distinction in your websites position and company your web site brings you.
The title pretty much says it all.
All I can say is it is truly, truly good. I would not alter a thing.
Thank you for another magnificent post. Where else could anyone get that kind of info in such a perfect way of writing’ I have a presentation next week, and I’m on the look for such info.
What’s the difference between Blog & Letter writing?
היי אני רוצה להמליץ לכם על סוכנות לטרייד אין, מכירה וקנית רכבים במחירים אטרקטיביים!
Blogging keeps me insane. Keep up all the positive work. I too love to blog. I found this one to be very informative.
I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful
I really like your writing style, fantastic information, regards for putting up : D.
I always was concerned in this subject and stock still am, thankyou for posting .I like this site very much so much excellent info .you have a link on my blog http://www.business4cityoflondon.co.uk/blog/?page_id=10
Attempt commenting frequently on other well-liked blogs (they generally ask you to fill in a website too). Make sure the comment is relevant to the post. It is better if you’ve read it, and have something insightful to say about it. Or comment on blogs which weblog about the same topics as you do.
iwboqbddk sowhuebd
I’d just like to telfvl you hvow much I learn from your writtings Liked u.Hope 2 be back soon for some more goodies