Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News jQuery: A new way to write JavaScript for rich web UI

jQuery: A new way to write JavaScript for rich web UI

This item in japanese


Created by John Resig in early 2006, jQuery provides the following main features to simplify JavaScript development:

JQuery Selectors

The basic premise in jQuery is a jQuery selector, which can be used to select elements from an HTML DOM and operate on them. For example, the following jQuery code snippet will show an alert message whenever a link on a page is clicked:

$("a").click(function() { alert("You are leaving this page!");});

$("a") is a jQuery selector. In this case, it selects all anchor elements. $ itself is an alias for the jQuery "class", therefore $() constructs a new jQuery object. The click() function is a method of the jQuery object. It binds a click event to all selected elements (in this case, a single anchor element) and executes the provided function when the event occurs.

Off course, you can apply filters if you want the alert to be shown only for certain links.

The traditional JavaScript will accomplish the same by:

<a href="" onclick="alert('You are leaving this page!')">infoQ</a>

With jQuery, we don't need to write an onclick for every single element. We have a clean separation of structure (HTML) and behavior (JavaScript).

Special Effects

Here is another example of a special effect feature in jQuery. The following code snippet looks for all paragraphs that have a class of "surprise", adds the class "shock" to them, then slowly reveals them:




Ajax Development

A common use of Ajax is to load a chunk of HTML into an area of the page. With jQuery, you simply have to select the element you need and use the load() function. Here's an example that updates some statistics:


The latest version, jQuery 1.1.3 has recently been released with DOM traversal over 800% faster than in 1.1.2. Other major enhancements include a re-written event system , with more graceful handling of keyboard events and a re-written special effects system.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Desktop

    by Kit Davies,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I'd be interested to know if there are any HTML/JS apps that have been written purely for the desktop and local deployment (backed by eg. Google Gears). I was previously sceptical of JS in the browser, but it seems the JS libraries are beginning to mature and I may have to eat my words!

  • Re: Desktop

    by Noah Campbell,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    On Windows, the .hta is an html application. You'll find it embedded in quite a few place, but perhaps not using the AJAX libraries for effects. It wouldn't be impossible since .hta will invoke an IE browser container.

  • JS wrapper for XUL and hta

    by karan malhi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I was wondering if there is a library which could wrap XUL and hta components. This would really allow to use rich widgets which are browser specific but using a common JS library.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.