4.3. jQuery

The jQuery library (available at www.jquery.com) is another library that does much more than simply Ajax communication, but at its small size (15KB), the extra features can be helpful in some situations.

Unlike the previous two libraries discussed in this chapter, jQuery aims to change the way you write JavaScript. It uses a querying interface to find specific nodes in the web page. The basic expression language used by jQuery is a mix of CSS selectors (from CSS Levels 1–3) and simple XPath expressions. Using this mix, it's possible to find specific nodes or groups of nodes without manipulating the DOM directly.

4.3.1. Simple jQuery Expressions

Since jQuery relies to heavily on its querying system, it's important to first understand how it works. The function used to query the DOM is $(), which accepts a string argument containing an expression. The expression may match one or many nodes in the DOM document, and the return value is another jQuery object that can be used to manipulate the result set (you won't be receiving any arrays or DOM elements back from most jQuery methods). This allows you to chain together jQuery methods into long groups of actions. Here are some sample queries:

//get all <p> elements
$("p");

//get the <div> element with an ID of myDiv
$("div#myDiv");

//get all textboxes (all <input> elements with type attribute equal to "text")
$("input[@type=text]");

As mentioned previously, each of these calls returns another jQuery object that can be used ...

Get Professional Ajax, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.