| Main |

Posted in:

I spent last weekend outdoors painting the outside of the building my apartment it’s in. It’s a big building, and the painting work seems to be a never-ending job. In order to motivate myself for the monkey work, I synced my iPod with the latest episodes of Dot Net rocks before I went outside.

I turned on the episode about the Java Script library jQuery with Rick Strahl. There been some fuzz in the blogsphere lately about jQuery (Fredrik Karlseth, Rick Strahl), fuzz I haven’t had time to catch up on before now. During the podcast I got more and more excited about this library. After I was done listening to the podcast, I immediately ran inside and sat down in front of my computer, downloaded jQuery and opened Visual Studio and created an empty web project. After a few minutes I had created the jQuery hello world, and I literally took my breath away, I was amazed. Let me do you a favor and introduce you to jQuery! - By the way I totally forgot to paint the rest of the day!

jQuery? What's thath? A new painting technology?

So what is jQuery?

Let me give you the elevator speech to get you up to speed. Java Script is quite easy to learn and use in small scale, but developing large and complex applications in this language and environment is hard. Therefore many developers tends to give the server alone the responsibility to rendering the HTML, and they use server side components like the ASP.NET Ajax Framework when they want to get fancy with AJAX functionality, but they rarely touch Java Script themselves. Why? As I said, complex applications in this language and environment are hard!

So what is jQuery? You probably heard of LINQ or SQL? Both of those are languages for performing dataset operations like selection, grouping, filtering etc. LINQ does it inside the .NET platform on CLR objects and SQL inside databases. I consider both of these to be so called DSLs (Domain Specific Language) which ease the work of dataset manipulation. I like to think of jQuery as a DSL to, not for datasets, but for manipulating the DOM (Document Object Model) inside the browser. It’s built on top of the Java Script language, and it provides you with some smart syntax and expressions to query the DOM. For instance, say you want to select all the link (a) tags in the webpage and assign a specific click event handler to them. You don’t need for-loops and the getElementById function anymore; you just write an expression to “select” all the link tags in the document, and voila you will get the instances to those objects.

Not only is it easy to manipulate the DOM, but it also has built in support for AJAX. You can actually do an AJAX call in one line of code and partial render the result into div object. Like a coworker of mine would have said; it is the icing on the cake. Not only is AJAX easy to program, but you don’t need to think about cross browser issues when using the XmlHttpRequest ActiveX component; the library will handle it for you.

Programming jQuery

Let’s get our hands dirty and do some programming. Let us start with setting up jQuery. Download the Java Script library from the jQuery webpage. The next step is to create HTML file and add a reference to the Java Script file in the header of the page.

Demo 1: Query the DOM (Document Object Model)

In this demo I will show you an example of how to write a simple expression (query) that contains a filtering option. We want to select all the objects in the DOM of type link (a tag) and filter out the links that contains an anchor. An anchor is a link that points to a resource within the HTML document (<a href=”#section”>anchor</a>). The last step in the demo is to override the click event handler by assigning it with a new callback function (line 10).

jQuery - demo 1

This samples was a very basic hello world, but hopefully I unveiled the power you get from this library to manipulate the DOM. If your minds start spinning then check out the API documentation, which will give you a great overview of what’s possible.

Demo 2: Ajax

Let’s begin with the form post scenario. Where I override the default submit functionality on the form object in order to avoid the post back to happen in the browser and last but not least do the post asynchronously (AJAX). This will avoid the page to flicker and reload itself.

jQuery - demo 2

The post-verb in the HTTP protocol will in most cases enter some data into a web application. Therefore you probably also want to do some partial rendering (update a particular area of the webpage) in order to get the latest data after the post is submitted. The code in the pictures below illustrates this in line 12.

jquery - demo 3

Can I combine jQuery with other technologies?

Yes, of course you can, but you have to redistribute the Java Script file with your application/web application. A technology I’ve been playing a bit with lately is the ASP.NET MVC (Model View Controller) Framework, and jQuery and this Framework is the perfect couple, but that’s another post. I also speculate on the possibility to use it when developing Windows Vista Sidebar Gadgets or Mac OS Widgets.

Where do I start?

The jQuery project has a very good webpage with tons of tutorials and demos. I recommend you to start there. I hope this inspired you to check it out, because jQuery is a library that can do the heavy lifting Java Script in your Web 2.0 applications!

Download demo source code

Tuesday, July 15, 2008 6:06:30 PM (W. Europe Standard Time, UTC+01:00) 
  Permalink  |  Comments [8]  |  View blog reactions  | 
Tuesday, July 15, 2008 11:18:28 PM (W. Europe Standard Time, UTC+01:00)
Great post Gøran, and good to see you blogging again!
Something to perhaps emphasize is the way you query for objects is all done through standard CSS 3. Because of this jQuery immediately clicked for Hege, who as a designer is used to do fairly complex CSS selections. I was a little bit surprised by the amazing flexibility in terms of selectors in CSS 2.1 and newer, as a “developer” you tend to limit yourself to element selectors (h1, h2 etc), id selectors (#myID) and class selectors (.class1, .class2), while you can do a whole lot more.

Say you got a div with two p’s in it. You can select the first one using this syntax: "div#intro p:first-child", or you can do a[href$=".pdf"] to select all links to PDF documents. Truly amazing, and this is all CSS 2.1 and 3.0. The cool thing is that selecting elements using CSS from JavaScript is introduced natively in IE8 and FF3, but isn’t supported in older browsers, and that’s the power of jQuery. If you’re running on IE8 you get better performance (since the selection is built in) but if you’re running on IE6 it will still work, as jQuery will traverse to DOM to find matching elements for you.

We (me and Hege) haven’t done much AJAX stuff with jQuery, but some of the things you can do with their effects library is just awesome! Smooth sliding animations by animating a bag of CSS properties over a certain time, all done in 5 lines of code. Just awesome! Will try to deploy the site Hege is working on tonight.

Cheers
Wednesday, July 16, 2008 4:04:53 AM (W. Europe Standard Time, UTC+01:00)
Welcome to the jQuery community. Be sure to hit up the mailing list:

http://groups.google.com/group/jquery-en

and follow us on Twitter at @jquery & @jqueryui

Also, don't forget to check out the jQuery UI controls and effects library.

http://ui.jquery.com

Rey
jQuery Project Team
Wednesday, July 16, 2008 1:30:09 PM (W. Europe Standard Time, UTC+01:00)
Nice post Gøran. I will definitely look into this library. :-)
BTW, I had some problems commenting this post....
Friday, July 18, 2008 7:13:20 AM (W. Europe Standard Time, UTC+01:00)
I also recommend everyone to have a look at the jQuery plugin repository: http://plugins.jquery.com/.
Friday, July 18, 2008 7:15:42 AM (W. Europe Standard Time, UTC+01:00)
Hello Ray!

Thanks for welcoming me to the jQuery community. I think jQuery is great; it’s made a solid impression on me. I’ve checked out the UI stuff, it’s fancy.

Twitter was down for maintenance atm, but I will follow these accounts. I’ve also joined your Google Group.

Keep up the good work!

Cheers

Friday, July 18, 2008 7:21:15 AM (W. Europe Standard Time, UTC+01:00)
Hi Jonas

It’s good to be blogging again. Had been thinking of that post for a couple of weeks, it’s hard to find time to do it. As always, thanks for the good comment!

Regarding your comment on CSS selectors, your point is good, and I’m totally agrees with you. A built in browser support is much more convenient in these cases where you want to select a specific object in the DOM and manipulate the presentation of it. But what about when you want to manipulate the functionality of those objects you select? Let’s say you want to change the onmouseout and onmouseover functionality of certain images on your page.

I don’t think I was clear enough regarding the query part of jQuery. Using jQuery for manipulating the presentation of an object is one thing (which I don’t recommend), but the real treat here is that you also can manipulate the functionality of those objects, the event handling overriding, and it’s here jQuery really shines. The expressions and query possibilities are just very nice “language syntax” that makes Java Script development more maintainable.

The other stuff that I didn’t mentioned with this library is the built in features for animations. I barely scratched the surface of this library, and it’s very extensible. It has this extensibility point which they call plugins. When you write your own plugin (which is basically a method) it will be hooked to the jQuery chain of objects. It’s like .NET Extension methods, where you can decorate a class with several new methods, same thing here, but only you decorating the existing jQuery API with new functions.
For instance let’s say I wanted to write a plugin that AJAXify a form, a form that can post data in the background without makes the page flickering. Then I could have had AJAX enabled a form with one line of JS, like this: $(“myForm”).AjaxMe(). This is also the way the animation features is implemented, they are basically plugins. So If I want to fade in and div it’s goes like this: $(“div”).fadein(“fast”).

Looking forward to have a look at the page you and Hege is working on. Give me a message when you have deployed it. Say hello to Hege from me.

Friday, July 18, 2008 7:27:11 AM (W. Europe Standard Time, UTC+01:00)
Hi Pål!
You should look into it, it blew my mind. It’s definitely one of the tools in my developer tool belt.

What kind of problems did you experienced when posting? I’m planning to change the blog engine I’m using. By the way, what engine are you using?

Saturday, February 20, 2010 11:52:28 PM (W. Europe Standard Time, UTC+01:00)
Excuse me. After the last of 16 mounting screws has been removed from an access cover, it will be discovered that the wrong access cover has been removed. Help me! Looking for sites on: Switching from lipitor to crestor dosage. I found only this - <a href="http://www.slcsystems.it/Members/Lipitor">mevacor versus lipitor</a>. Then the pharmacy is the vatican, lipitor. Reabsorbed place sells true allergic operation by eligible synapses and own today, lipitor. With respect :cool:, Wiley from Guatemala.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):