Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


jQuery, ASP.NET, and Interoperability

Microsoft's announcement that it will contribute to the development of the jQuery JavaScript library and enhance interoperability between jQuery and ASP.NET caught a lot of developers and project managers off-guard, causing them to play catch-up in regards to what jQuery is and howit can play an important role in enterprise projects.

The idea behind jQuery is to simplify the task of getting a selected subset of DOM elements to work with. In other words, the jQuery library is mostly intended to run queries over the page DOM and execute operations over returned items. But the query engine behind the library goes far beyond the simple search capabilities of, say, document.getElementById (and related functions) that you find natively in the DOM. The query capabilities of jQuery use the powerful CSS syntax which gives you a surprising level of expressivity. For example, you can select all elements that share a given CSS class, have a given combination of attribute values, appear in a fixed relative position in the DOM tree, and are in particular relationship with other elements. More importantly, you can add filter conditions and chain all queries together to be applied sequentially.

The root of the jQuery library is the function defined as follows:

var jQuery = window.jQuery = window.$ = function( selector, context ) 
   return new jQuery.fn.init( selector, context );

Nearly any jQuery script is characterized by one or more calls to the $ function -- an alias for the root jQuery function. Any line of jQuery code is essentially a query with some optional action applied to the results.

When you specify a query, you call the root function and pass it a selector plus an optional context. The selector indicates the query expression; the context indicates the portion of the DOM where to run the query. If no context is specified, the jQuery function looks for DOM elements within the entire page DOM. The jQuery root object performs some work on the provided arguments, runs the query, and then returns a new jQuery object that contains the results. The newly created jQuery object can, in turn, be further queried, or filtered, in a new statement as well as in chain of statements; for example:


The call selects all DIV tags with a CSS class attribute of Tooltip. Written that way, however, the code has no significant effect. The $ function selects one or more DOM elements and that's all of it. It just returns a new jQuery object that contains the DOM elements. The resulting set is known as the "wrapped set". You can grab the size of this set by calling the size method, as shown below:


Any function you invoke on the wrapped set is called for each element in the set. For example, consider the following code:


The query selects all DIV elements currently hidden where the class attribute equals Tooltip. Each of these hidden DIV elements is then displayed using a fade-in algorithm that takes half a second to complete.

You can also loop over each element in the wrapped set using the each function:

    function() {

The each function gets a JavaScript callback function and plays that function for each element. The difference between the function each and a manual JavaScript loop lies in the fact that the function each automatically maps the this object (as in the snippet) to the element in the collection being processed. The callback function, however, also receives an integer parameter being the 0-based index of the iteration. If you are interested in using this piece of information, you just add a parameter to the definition of the callback passed to each.

    function(index) {

As you can see, most of the time by simply calling the function directly on the wrapped set you obtain the same effect as writing the loop yourself. The each function is reserved for special situations where you need to employ some application-specific logic to determine the action to take.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.