Prototype is comfortably installed in a lot of developers mind because of two reasons : it was the first widely used and script.aculo.us
. Being the first accepted library is a hard thing to do and it will always help them. Scriptaculous (I hate to write it with the dots) had an incredible effect on the popularity of prototype. For the first time, we could do “flash animation” without using flash. This was instant popularity.
jQuery is one of the bastard child of prototype. It reuses the $() function
but brings it to a level you never could have hope for. In fact, jQuery is almost completly based on the $ sign. Why? Maybe because it's fast, maybe because it's trendy. I don't know. Example, you want to download a JSON from a server, $.getJSON(“http://www.thesite.com/thejson”)
Reason #1 : The selectors
jQuery supports a lot of selectors. And when I say a lot
, I really mean a lot
. First, there's CSS selectors
. You just have to apply what you know about CSS and you can write many complex selectors.
Second, there's XPath selectors. When I was young (sic!), I wrote a lot of XSL. And when I say a lot, I really mean a lot. XSL makes an intensive use of that XPath thingy. Because XHTML is basically XML, we could use XPath to easily navigate throught the DOM. The problem was that before jQuery, nothing could do that. Now, we can!
In comparison, prototype just has the $('theId') to select a element. Ouch!
Reason #2 : The Attributes
It's easy to add/remove attributes
to any HTML element. I use it when I create new elements or when I need to add/remove a css class on an element. The interesting thing is that you can chain
them and it will work. Example, I want
$("div").attr("title", "This is a div").addClass("newClass");
You can add as many as you want…
Reason #3 : Ajax and JSON
Once again, ajax
is now easy as 1,2,3. I wrote an article on JSON
and in my mind, there's no real alternative to JSON. jQuery helps you with it.
alert("I received the json and put it in the json var : " + json.toString());
It's a little more complex with prototype