Learn a Few JavaScript Libraries at Once

Learn 3 Excellent JavaScript Libraries at Once

Let’s go with a hands-on approach today. We are going to write a tiny snippet of code in jQuery and then port it over to several different libraries. Outside of that chunk, we are going to look at how to implement some basic, necessary functionality in each of these libraries. Sounds fun? Let’s dive in!

A Word From the Author

JavaScript frameworks are a boon when you have lots of ideas to turn into functional, clean code. And with the meteoric rise of web applications using AJAX technology, frameworks like jQuery are necessary to reduce the time you spend implementing the required functionality. And looking around you see jQuery this and jQuery that with little to no mention of other frameworks. Fact is, there are a ton of other frameworks which are just as elegant as jQuery. Today we are going to take a look at two frameworks which promise to make writing code easier. Do note that I use the words library, framework and toolkit interchangeably. I am not going to go into semantics and neither should you. Each of these libraries try to accomplish different things. We are only going to look at the parts where their intended functionalities overlap.

Basic Functionality

Outside of esoteric and/or dedicated apps, most people’s JavaScript code can be broken down into chunks which take care of the following functionality:

DOM ready

A JavaScript developer runs into this problem sooner or later: his pristine code just won’t run. He has debugged the code again and again but to no avail. The problem is that the code is just placed at the top of the page. When the script is executed, the elements he is referring to in his code don’t even exist in the DOM leading to these errors.

Remember, script calls are synchronous. This means that when a browser sees a script tag, it ceases loading everything else until the scripts are loading. This is in stark contrast to its general loading mechanism where other objects are often loaded asynchronously.

You could always work around this by just placing the code at the bottom of the page but you never know. With this in mind, most libraries provide a DOM ready function to make sure the code is only executed after the DOM is ready to lock n’ load but before the images are completely loaded.

With raw JavaScript, you’d be looking at something like this.

window.onload = someFunction;

Accessing elements

You obviously want to access specific elements and manipulate them somehow. Unfortunately, IE is rather finicky and can break your code. To let developers write better code, each library provides a cross browser method which lets you access a specific element. Most libraries use CSS style selectors to zero in on their target elements to make the learning curve shorter and much more importantly, cleaner looking code.

Without a framework, you’d have to do this:

var elem = document.getElementById('header');
var io = document.getElementsByTagName('input');

Manipulating elements

Of course, once you’ve obtained the element, you’d want to perform some operation. This includes adding or removing a class, toggling its visibility, changing its dimensions, editing its contents and so on. As always writing all this in raw JavaScript can be painful. Each of these libraries provide wrapper functions to do all of the above noted work and lots more.

With raw JavaScript, your code would look like so:

document.getElementById("title").innerHTML="Title";

Manipulating the DOM

Often, you’d want to directly change the nodes in the DOM. You might want to create a new wrapper object to which you want to put your recent Twitter status in or you might want to remove a div element for an email app you are writing. In each of these cases, you’d want to manipulate the DOM efficiently and all of these libraries provide methods to do so in a clean manner.

Appending an element would take this much code with raw JS.

var myElement = document.createElement("<div>Sometext</div>");
document.all.myBody.appendChild(myElement); 

Hooking up events

Events are the building blocks of any application and one of the more peskier parts of cross browser scripting. The thing is, W3C defines one way and IE does its own thing. To over come this, all of these libraries provide ways to attach or unattach even handlers to specific events of the element.

Hooking up events in raw JavaScript.

element.addEventListener('click',doSomething,false)

AJAX request

Native JavaScript using the XHR object is tedious to write and debug. In order to let developers write code more easily each of these frameworks abstracts the tedious details of implementing an AJAX request behind a simple function call with methods to call when the request succeeds or fails.

I am not even going to try and post some code to make AJAX request with raw JS. Instead you should look at Jeffrey’s excellent screencast. Shortly, we’ll see how a framework drastically cuts down your code.

The Functionality we are Looking to Code

Nothing fancy really; we’ll build a simple demo. The markup looks like so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Net Tuts</title>
</head>

<body>
<p id="text">This is some sample <span id="maintrigger">random</span> text. Click on the word random 
to modify the DOM by creating a new element and appending it to the current content. 
</p>
</body>

</html>

We have some text within a p element. We have the word random wrapped in a span element with an ID of maintrigger. Every time an element the element with an ID of maintrigger is clicked, a new div element needs to be appended to the paragraph element containing the text “clicked”. This simple demo touches all of the basic functionality requirements including accessing elements, hooking up events and appending elements except that of AJAX abstraction and will let us get a feel for each library.

Before we look at the other libraries, it’s best we look at how jQuery lets us do each of them.

jQuery

Ah, all pervading jQuery. It has been the talk of the town among web developers for a while know and rightly so. jQuery doesn’t try to do too many things. You aren’t going to find a dozen plus widgets bundled with the base build or ways to support classes or class based objects. jQuery focuses primarily on the DOM and I think they’ve done an excellent job.

If your work is primarily DOM related, jQuery is the way to go. The syntax is among the easiest and a lot of times it feels like reading pseudocode than actual, working code. The community is big and friendly and there are a ton of plugins you could just drop in for added functionality. Plus there is the UI library if you want to add some common widgets to your site.

jQuery’s user base includes:

  • Google
  • IBM
  • Microsoft
  • Amazon
  • Dell
  • Twitter
  • ESPN
  • Digg
  • Netflix
  • Mozilla
  • WordPress

Extremely impressive if you ask me.

Now, we’ll look at how jQuery lets us reduce the time we spend coding before we look at the other frameworks.

DOM ready

$(document).ready(function () {
    // Your stuff here
});

When writing your jQuery code it is imperative that you put your code inside this section. Here we ask the code to be executed when the DOM is ready to be manipulated. You can either pass in a functions name or just write all your code inside an anonymous function as in the example above.

Accessing elements

var container = $("#main");
var posts = $(".post");
var headings = $("h1");

Simple as it gets. We first create a variable to hold a reference to the DOM node since we don’t want to look for it again and again. $ is an alias to the jQuery namespace. We just pass in the id, class or tag of the element just as you’d do if you were writing a CSS declaration. Passing in CSS selectors works just as you’d expect. Examples like the below work too.

$("span:has(a)").doSomething();
$("ul li:nth-child(3)").doSomething();
$("p:hidden").doSomething();

Manipulating elements

$("div").attr("class", "clicked");
$("div").html("<span>Hello</span>");
$("span").addClass("current");

Changing an elements attributes, its contents or the classes it has are fairly trivial. We just access the required element and call the necessary function. The attr method lets us change a specific attribute of an element, the html method lets us specifiy the HTML content and the addClass method needs no explanation.

Manipulating the DOM

$("#post").after("<div>Comments</div>");
$("p").wrap("<div></div>");
$(".comment").append("<span>Posted by</span>");

Adding elements after the specified element, adding content inside an element or wrapping the passed element with another is just as easy. We obtain the element and then call the function which best suits our need.

Hooking up events

// Using anonymous functions
$("#submit").bind("click", function(e){
// Your code here
});

// Passing in a functions name    
$("#submit").bind("click", evHandler);

Hooking up events is similarly easy. We obtain the element and then call the bind function. The first argument is the event type and the second is the code to execute when the event is triggered. You can either pass in the function’s name or just create an anonymous function and place all your code inside that.

AJAX request

$.ajax({
  type: "GET",
  url: "test.html",
  success: function(xhr){
    //Some code here
  },
  error: function(xhr){
    //Some code here
  }
  
  $.ajax({
  type: "POST",
  url: "test.php",
  success: function(xhr){
    //Some code here
  },
  error: function(xhr){
    //Some code here
  }

});

You only need 3 lines to make a barebones AJAX call. Specify the type of request, the URL and you are good to go. Success and error functions can be defined to specify what happens if their namesakes occur.

Equivalent code

To achieve the desired functionality mentioned above, your jQuery code would approximately look like so:

$("#maintrigger").click(function () { 
      $("p").after("<div>Clicked</div>");
    });

3 lines is all it takes. We select the necessary elements, call the click function, create an anonymous function and append a div element. It sounds a lot more complicated than it actually is really.

Now that we’ve looked at the jQuery code, we can explore the other libraries.

Prototype

Prototype is the grand daddy of JavaScript frameworks. It provides all the niceties you’d expect off a mature framework and then adds a little more. It also provides a number of library functions to help you write nicely abstracted, object oriented code.

Prototype’s user base includes:

  • Apple
  • NASA
  • 37 Signals
  • hulu
  • Rails
  • Backpack
  • Gucci
  • last.fm
  • Wired
  • Prada
  • Dreamhost

A lot of well known, top tier names there. As I said, Prototype used to be the framework of choice for a long time before jQuery came in.

Now that the introductions are done, let’s see how Prototype can help you write better JavaScript.

DOM ready

document.observe("dom:loaded", function() {
  // Your code here
});

The above is Prototype’s version of DOM ready. It looks weird at first if you are coming from jQuery but it is just as simple. The observe function listens for the passed event for the lifetime of the document. We just pass in the necessary event and envelop all our code inside an anonymous function just like with jQuery.

Accessing elements

//Access an element with an id of post
$('post');

//Use the CSS selector engine
$$('.comment');

$ provides an alias for the document.getElementById method. It lets you find elements with the passed ID value.

$$ lets you use CSS selectors instead. It takes as it’s arguments any number of CSS selectors and returns the specific element or an array of them. Just as with the jQuery selector engine, you can use all sorts of nefarious CSS3 selectors including child selectors, attribute selectors and even pseudo classes.

Manipulating elements

$('post').addClassName('author');
$('container').getWidth();
$('post').hide();

Prototype provides a number of powerful method to work with the returned element. Remember, you need to access this through the $ object. Which means you first need to save the reference before you can manipulate the element in what ever way you see fit.

Manipulating an element is as simple as obtaining a reference to the element and calling the necessary function. You can do lots of things from setting sttributes, to hiding the element.

Manipulating the DOM

$('post').wrap('div', { 'id': 'container' });
$('post').update('<p>Some random text</p>');
$('post').insert('div', '<p>Some random text</p>');

The first method wraps the passed element with a described element settings its various properties in the process. The update functions replaces the content of the passed element with the one we want. Insert inserts plain text or HTML snippets at the top, bottom, before or after the element. Instead of using separate methods like append and after as in jQuery, we just need to specify the position and we are done.

Hooking up events

// Using anonymous functions
$(btn).observe('click', function(event){
//Your code here
});
 
// Passing in a function's name 
$(btn).observe('click', doSomething);

As I mentioned before, the observe function lets you hook up events to their event handlers. We first obtain a reference to the element and then call the observe method passing in the event’s name and function as parameters. If you don’t want to create a separate function just for this, you are always free to create an anonymous function and put all your code inside there.

AJAX request

new Ajax.Request("test.html", {
  method: 'get',
  onSuccess: function(transport) {
  // Some code here
  },
  onFailure: function(transport) {
  // Some code here
  }
});

new Ajax.Request("test.php", {
  method: 'post',
  onSuccess: function(transport) {
  // Some code here
  },
  onFailure: function(transport) {
  // Some code here
  }
});

Prototype provides extensive wrapper functions for making an AJAX request. I’ve shown the lowest level AJAX POST and GET requests here. There are a ton of other specialized methods for AJAX requests including an auto updater.

Equivalent code

To achieve the desired functionality mentioned above, your code would approximately look like so:

$("maintrigger").observe('click', function(event){
  $("text").insert('<div>Clicked</div>');
 });

Still a 3-liner. What we are doing is similar to the jQuery code, the syntax is just different. We use the observe function to attach the click event to the code we created in an anonymous function. We just insert some text to denote that the process was a success.

You’ll see that generally we are doing the same thing jQuery does, just with a different syntax and a few differences. When your work is not DOM-centric and you need proper objects to properly leverage your ideas, Prototype is the framework to choose.

Mootools

Mootools doesn’t profess to be an easy framework to learn. Let’s face it, its web page says it is a web application framework for intermediate to advanced web developers. Don’t let them fool you. It is an extremely elegant framework which lets your create extremely classy JavaScript. It focuses on JavaScript as a whole instead of just the DOM. With that in mind, it provides a number of functions to speed up your workflow and also extends the DOM wherever possible. And just like Prototype, it contains a class creation and inheritance system which should make those coming from C# or Java more comfortable with JavaScript.

Companies which use MooTools include:

  • Ferrari
  • Vimeo
  • Palm
  • Nintendo
  • Formula 1
  • GameSpot
  • CNet
  • Joomla
  • phpMyAdmin
  • Jeep
  • Bing

Another framework with a very impressive user base.

Let’s now look at how MooTools makes your life as a JavaScript developer easier.

DOM ready

window.addEvent('domready', function() {
    // Your code here
});

It might look complicated but don’t fret. It just looks different. We attach the domready event of the windows to the anonymous function. The addEvent lets us attach events to their handlers. MooTools defines the domready event which we make use of here. As usual we wrap all our code in an anonymous function and place it inside. There! That wasn’t so hard, was it?

Accessing elements

// Use the $ function
$('someElement');

// Use CSS selectors
$$("#main");
$$(".post");
$$("h1");

Just like with Prototype, you can use the $ function as an alias for direct retrieval or use the $$ function to use much more intuitive CSS selectors.

Each of these methods return either a DOM element or an array depending on which you use.

Manipulating elements

$('someElement).hasClass('clicked');
// Returns true if the element indeed has that class

$("someElement").setProperty('class', 'clicked');

$("someElement").empty();
// Empties the element of all its children

MooTools provides a number of methods to manipulate a specific element including setting its attributes, changing its contents and so on. If you are interested, you should consult the MooTools documentation here

Manipulating the DOM

var someElement = new Element('div', {id: 'mySecondElement'});
someElement.inject(someOtherElement);
// Injects the contents of someElement within someOtherElement

$("someElement").destroy();
// Removes element and all its children from the DOM

$('someElement').clone().
// Makes a copy of the element

Just like most libraries, MooTools provides a plethora of functions to let us modify the DOM. Everything from appending content to completely removing a node from the DOM is included.

Hooking up events

// Using anonymous functions
$('myElement').addEvent('click', function(){
    // Some code
});

// Passing in the functions name
$('myElement').addEvent('click', doSomething);

As I noted above, we use the addEvent method to attach the event to its handler. We pass the name of the event to the function and as usual we are free to choose between creating a separate or anonymous function to put our code into.

AJAX request

// A GET request
var myRequest = new Request({method: 'get', url: 'test.html'});
myRequest.send();

// A POST request
var myRequest = new Request({url: 'test.html'});
myRequest.send(someData);

Setting up an AJAX request is similarly easy. MooTools provides a robust Request class which lets use make POST or GET AJAX requests. The default method is POST so there is no need to specify if you are making a request.

Just like other frameworks, the request class supports callbacks for success, error and completion.

Equivalent code

To achieve the desired functionality mentioned above, your code would approximately look like so:

$$("#maintrigger").addEvent('click', function(){
   var elem = $("text");
   var someElement  = new Element('div');
   someElement.appendText("Clicked!").inject(elem, "after");
});

Slightly more verbose than the other 2 implementations but here we actually create a div node from scratch and then append it. Also we store a reference to the paragraph element itself. After that, we simple add the necessary text into the newly created element and then append it to the DOM.

More verbose? Yes. More difficult? Definitely no. It is just a different style of coding.

Code Comparison

jQuery

$("#maintrigger").click(function () { 
      $("p").after("<div>Clicked</div>");
    });

Prototype

$("maintrigger").observe('click', function(event){
  $("text").insert('<div>Clicked</div>');
 });

MooTools

$$("#maintrigger").addEvent('click', function(){
   var elem = $("text");
   var someElement  = new Element('div');
   someElement.appendText("Clicked!").inject(elem, "after");
});

All of these snippets of code essentially do the same thing. Just in different styles and with differing amounts of control.

Experiment

This article was in no way intended to compare these frameworks. Instead I wanted to call the reader’s attention to the other viable solutions available outside of jQuery. It’s not that jQuery is bad. I love jQuery, it is a wonderful framework but it primarily focuses on the DOM and DOM alone. If you are looking to write a web application or a renderer or some other cool thing, creating maintainable, extensible, clean code is a priority. It is for these kinds of scenarios when it make more sense to resort to another framework.

I could choose only two frameworks in this article due to space constraints. There are a number of other, just as powerful, frameworks including Dojo, YUI, Rico and GWT which also deserve your attention.

Hopefully I’ve piqued your interested in alternate JavaScript frameworks today and am truly hoping you are going to experiment with other frameworks. Let us know how the experimentation goes in the comments. Happy coding!


Siddharth is Siddharth on Codecanyon
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://james.padolsey.com James

    Your MooTools is a little too verbose IMO…

    Here’s a one liner:
    new Element(‘div’, {html:’Clicked!’}).inject(‘text’);

    Also, why are you selecting the paragraph via its tag-name with jQuery but with MooTools and Prototype you’re using its ID?

    Oh, and what is this? -

    document.createElement(“Sometext”);
    document.all.myBody.appendChild(myElement);

    I believe that should be:

    var div = document.createElement(‘div’);
    div.appendChild(document.createTextNode(‘Sometext’));
    document.body.appendChild(div);

    • http://www.ssiddharth.com Siddharth
      Author

      Thank you for pointing this out. I seem to have sent in an earlier version of the code and the accompanying text. My bad. :)

    • http://www.alfystudio.com Ahmad Alfy

      There are also some typos :
      in MooTools:
      $(‘someElement).hasClass(‘clicked’);

      an (‘) is missing in $(‘someElement)

      I really appreciate your work
      It’s really great that some1 took the chance to make this comparison :)

  • Jack F

    Fantastic! One of the best yet IMO. Thanks

  • http://www.craniumdesigns.com Steve Davis

    Nice article! As a frontend / UI guy, I don’t see much need to use anything besides jQuery, as i am usually only trying to mess with the DOM. Good to know I have options if I ever decide to delve into something super complicated.

  • http://www.freshclickmedia.com Shane

    I’m sure there will be a debate about which library is ‘best’. I was using Prototype about three or so years ago, but later discovered jQuery.

    Whilst it might be intersting to see how different libraries achieve similar abstractions, jQuery, for me, does it all, so, I’ll stick with that. Its amount of users, quality of documentation, and wealth of plugins means that it’s now the de facto JavaScript framework.

    For me, anyway, a look at how Prototype and mootools do things is of minimal interest.

    • http://www.newarts.at Drazen Mokic

      There would not be a debate if people would understand that each of this frameworks aims for another main goal.

      jquery`s main goal is simply DOM manipulation and MooTools`s one is object oriented javascript.

      The question should not be which framework is better. It should be what you want to do?

      The right tool for the job.

      • http://www.freshclickmedia.com Shane

        Sure, but if somebody has learned to use all 3 (I suspect this is the minority), that person would normally gravitate toward a particular framework, based on personal preference, no?

        For example, I used Prototype, thought it good, and then learned jQuery, thought it better. Now I stick with jQuery, since it’s what I know best, and does what I want, and does it very well.

      • http://www.newarts.at Drazen Mokic

        A person who learned all 3 would use the right framework for the job. He would know the strenght and weakness of each framework and choose the fw which fits best for that project.

        Why should he else learn all 3?

  • http://www.giulianoliker.com Giuliano

    Probably, most useful and compact article I ever read on Nettuts.

    Thank you Siddharth

  • daron

    Oh no you’ve forgotten to add a ‘ in Mootools > Manipulating elements

  • Marcus

    I think it would have been nice if the article had shown each of the libraries strengths instead of showing how to do the same thing with all three libraries in the area where jQuery is at its strongest.

    Showing us why, for example, MooTools would be a better choice for building a web application might have gotten some of us jQuery fanatics to take a peek over the fence.

    • http://11heavens.com/ Caroline Schnapp

      Agreed.

    • http://www.giulianoliker.com Giuliano

      I think that was not the purpose of this article, this is more like basic introduction for beginner designers/developers.

      It is impossible to say that one of the frameworks is the best for specific project. I’m sure there would be a lot of people with arguments against. Basically, you can finish 95% of the project with any of frameworks mentioned in article. I prefer jQuery because it is most popular and I prefer products with bigger support community.

      • http://www.ssiddharth.com Siddharth
        Author

        Guiliano nailed it. This is intended as a quick start guide. I wanted to write this mainly because a couple of recent projects I’ve did absolutely wanted me to use ProtoType for all the JavaScript. I remember googling a lot just to find out how to do basic stuff with that ProtoType.

        If this has saved someone at least a minute of their time, I am happy. :)

      • http://www.ssiddharth.com Siddharth
        Author

        Gah. Spelling and grammar errors in my previous comment. Remind me not to post anything after having just woken up.

  • http://www.dsaportfolio.com.br Diego SA

    jQuery seems to be the simplest among the three, because of the quantity of lines needed to make it works. Even so, I’m used to the MooTools.
    This tutorial will help me a lot. Thanks!

  • http://www.vistacompany.org mohamed rootshell

    wow!
    i like jquery
    thanx Siddharth and also jeffery for his great tutorials that helped me tooooooooo much

  • Robert DeBoer

    Great article and I agree that really, no library “is the best.” If you compare libraries with the same goal, then one will be the best – but each one has a different goal in mind. So you need to choose the one that best fits your applications need.

    I do agree that it would of been cool to see examples of each ones strength’s instead of how to do the same thing in all three. It does sort of say “each one is basically the same, so just stick with what you got.”

    • http://11heavens.com/ Caroline Schnapp

      Author concludes:

      “If you are looking to write a web application or a renderer or some other cool thing, creating maintainable, extensible, clean code is a priority. It is for these kinds of scenarios when it make more sense to resort to another framework [than jQuery].”

      But he does not prove his point in his article.

      That’d a great subject for another article!

    • http://11heavens.com/ Caroline Schnapp

      So, I agree with you, Robert.

      • http://www.ssiddharth.com Siddharth
        Author

        Writing an article about why MooTools is better for web apps would entail a lot of object oriented JavaScript. I am not sure that would click here.

        If interest peaks, I’d definitely do an article on which areas MooTools shines.

  • http://nouincolor.com Oskar Krawczyk

    For MooTools, it’s actually: el.set(‘class’, ‘clicked’);

    Regarding the “more verbose” part– yes, indeed it can be more verbose when you need it to be, otherwise with a few lines of code you can implement your own el.observe() or el.click().

  • http://technetic.org Paul

    pretty good foundations. i was waiting for something like this. thanks.

  • Darren

    @Siddharth, I really think you should change your last example to the one liner that James gave in the first comment to be fair to MooTools. There is absolutely no reason why you would spread the MooTools function body over three lines and the other two examples over one (as James has demonstrated). Could you equally have done (something) this for jQuery:


    var elem = $('p');
    var someElement = $('Clicked');
    elem.after(someElement);

    • http://www.ssiddharth.com Siddharth
      Author

      I agree. I’ve sent in an updated version.

      With respect to the jQuery code, it’s a matter of different programming styles I guess. :)

      Thanks for the input.

  • The Chonchilla

    You should consider compiling some of your elaborations for a book. You’ve obviously taken the time to consider your audience as your article is thoughtfully approached. We appreciate your time and efforts. Good read man!

  • Ananda Rudra

    I think jquery is simply the coolest. both on the basic of ease of writing and performance, and not to forget the huge plugin repository.
    Check this to have a glance at the performance comparisons
    http://bit.ly/D2UQ
    http://bit.ly/knBK8

  • http://laranzjoe.blogspot.com lawrence77

    3 in 1…. cool…

  • http://speed-co.de Felix

    Nice Article.
    I wrote my Bachelor over JS Frameworks.
    You can have a look at my comparison under:

    http://pwn.it/tmp/bouncing_balls/balls_jquery/
    http://pwn.it/tmp/bouncing_balls/balls_mootools/
    http://pwn.it/tmp/bouncing_balls/balls_scriptaculous/

  • http://thoughtsunlimited.net/blog Ashwin

    Excellent article! Easily written and covers lot of ground for people like me… One of the best articles I have read in the recent times.

    Definitely bookmarking it for the weekend

  • http:/giorgiosironi.blogspot.com Giorgio Sironi

    I use Dojo… :)

  • Nook

    I’m new to this, what does they mean with DOM Ready???

  • http://www.demogeek.com DemoGeek

    IMO, jQuery stands out (obviously!) but given the fact that Rails plays nice with Prototype (cite: AWDR book) I might have to get that on to my already exploding head :(

  • http://www.slojoomla.si Sass

    Great article.
    I like jQuery, but at the other side, i never try to work with Mootools or prototype.

    thanks

  • http://www.lornepike.com/canadian-website-design-internet-marketing.shtml Lorne Pike

    Great post. I see myself focused on jQuery for quite some time yet. It was one of my happiest development days when I first saw it. But it’s interesting to see how these three fit together. Thanks, Siddharth!

    • http://www.ssiddharth.com Siddharth
      Author

      Glad to be of help. :)

  • http://www.devluv.com Ricardo Vega

    IMO, Prototype and Mootools should be rewritten, both are very very intrusive to core Javascript scope.

    I’d love to say that the three of them are equally good, but thats not true.

    http://www.mooforum.net/help12/jquery-and-mootools-conflict-t328.html

  • http://www.kiran13xtreme.co.uk Kiran

    Must say intresting and knowlagable article! Keep them coming!

    • http://www.ssiddharth.com Siddharth
      Author

      What would you like to see next?

  • doryanbb

    Thx for sharing :>

  • http://11heavens.com/ Caroline Schnapp

    It looks like the Prototype equivalent really is:


    document.observe('dom:loaded', function () {
    $('maintrigger').observe('click', function(event) {
    $$('p').insert({after:'Clicked'});
    });
    });

    • http://11heavens.com/ Caroline Schnapp

      Mmm… how do I put code in here again? Using the code tags don’t work. Some HTML within the code tags got parsed. Aggravating.

    • http://11heavens.com/ Caroline Schnapp

      document.observe('dom:loaded', function () {
        $('maintrigger').observe('click', function(event) {     $$('p').insert({after:'<div>Clicked</div>'});
        });
      });

    • http://11heavens.com/ Caroline Schnapp

      Alternatively:

      document.observe('dom:loaded', function () {
        $('maintrigger').observe('click', function(event) {
          $$('p').insert('<div>Clicked</div>', 'after');
        });
      });

      The problem with your Prototype Equivalent is that you’re doing an append with it. ‘bottom’ is the default position for Element.insert() when not specified. Whereas with jQuery you are clearly inserting the element after the paragraph as its next sibling.

      • http://maveno.us Dustin Hansen

        Very true, he should have used .append(‘Clicked’) as opposed to .after(…).

  • Ramesh

    Thanks for the nice articles

  • http://observations.udkology.com/ udkl

    Very very good write up ! I appretiate the efforts put into writing it.

  • Edison

    Thanks so much for that, I can finally start taking those jquery sample code and translated to prototype. Huge help

  • Simon

    Would be interesting to see what you think of the JavaScript Library that the BBC have been using internally and have just made public:

    http://www.bbc.co.uk/glow/

    • http://maveno.us Dustin Hansen

      Lacking.

      Good start, but still quite lacking.

  • http://www.axomedia.ch axomedia

    Awesome! I love that one. Please go ahead with this nice tuts!

  • http://spotdex.com David Moreen

    Amazing article I really appreciate it. I am going to start to drift slightly away from jQuery and maybe start to learn mootools. No matter what though jQuery will always be a fall back!

    • http://www.ssiddharth.com Siddharth
      Author

      Glad you liked it. Post back if you run into any difficulties.

  • Mark

    check jqueryvsmootools.com for a better comparison of the frameworks and how to accomplish tasks in each

    • http://www.ssiddharth.com Siddharth
      Author

      I note early in the article that this is NOT a comparison. Just a quick start guide.

      That page compares rather advanced features. This looks at just the basics.

  • jem

    It’s nice to see a lot off Mootools fans on nettuts! Always seems to be nothing but the ole jquery showing up on here so i begin to wonder!

    I’ve written some plugins with jQuery before, and overall I think its a very nice framework. But, my heart lies with Mootools as I find it the more *complete* framework for working with javascript. There’s dozens of native extensions available that simply aren’t in jQuery, and I find myself having to copy and paste methods from the Mootools core every time I work with jQuery. Plus it just makes me feel like I’m working in an actual object oriented language (and i am!) when I work with Mootools.

    This article was very intriguing to see how some things are done in other frameworks, prototype in particular. Good work!

  • manju

    I would like to know how to upgrade mootools. I am using the mootools 1.11 version and need to upgrade to the latest version (1.2.3)

    Thanks

  • Dharshana

    Great Stuff..Thanx

  • hubeRsen

    nice article, thanx

  • http://www.sthelse.com/ So.So.Sorry

    Pretty nice!

  • Parker

    I wonder why someone will leave a well documented jQuery for anything else
    just be curious not even JavaScript Library used by BBC

  • http://... Agam

    In Moo tools->Manipulating elements, You forgot to put : ‘, it should look like this:(on line 1), after someElement

    1. $(‘someElement’).hasClass(‘clicked’);
    2. // Returns true if the element indeed has that class
    3.
    4. $(“someElement”).setProperty(‘class’, ‘clicked’);
    5.
    6. $(“someElement”).empty();
    7. // Empties the element of all its children

  • http://www.brettwidmann.com Brett Widmann

    This is great! Thanks for sharing.