jQuery

Some weeks back I came across jQuery. Once in a while a library comes along that makes you say “wow”. To me jQuery is among those. jQuery is a JavaScript library that allows you to perform complicated (or sometimes not complicated but boring) JavaScript work in just a few lines.

What’s the fuss about jQuery! Lets say you want to do the following:

  1. When the page is loaded, draw some tabbed panes.
  2. When the page is loaded, make an AJAX call to get an RSS xml file and then parse through the XML response and display all blog titles.
  3. When a link is clicked you want some list items (in li tags) to turn blue via css manipulation.
  4. And you want to display the number of paragraphs within a div.
  5. Last you want the div to hide (slowly).
  6. Oh also when you click on the tabs you want to print out the current time.

Now imagine all of this on a single html page (small page). That is a fair bit of JavaScript there. With jQuery I put all of that on a single html page so you can see its power.

Here is the complete html code…save it to file test.html. Note that I include the latest js code directly from jQuery site. If you download the library then change the script include paths appropriately.

Download the atom.xml file and copy it to the same folder as test.html.Fire up test.html in your browser.

A few pointers:

  • $(“#orderedlist > li”) means return the li elements inside the element with id ‘orderedlist‘.
  • $.ajax is the call to the ajax function. If you are familiar with JSON you should be fine reading that code.
  • $(xml).find(‘entry’).find(‘title’).each(function(){….}parses the AJAX XML response. Finds all entry/title elements and for each executes the anonymous function.
  • $(“a”).click(function(){….} attaches an anonymous event handler function to ALL link tags.
  • $(“a”).addClass adds a style class to all link tags.
  • $(“#mycontainer p”).size() means count the number of<p> tags inside the div element with id ‘mycontainer’.
  • $(“#numparas”).text means replace the text inside.

There is the jQuery library and then there is the jQuery UI library.The tabs example above uses the jQuery UI library. There are other UI widgets built around jQuery and I like what I see. Though I wish the table supported inline editing of cells. There is a plugins that allowed editing but I will better if the main table code supports editing.