Understanding the Context in jQuery

Wednesday, June 24, 2009

When selecting elements jQuery has an optional second argument called context. This context provides a means to limit the search within a specific node. This is great when you have a very large DOM tree and need to find, for example, all the <a> tags within a specific part of the DOM tree. I’ve seen various articles, usually performance related, stating that you should be using a context for your selectors. While it is true that using a context for your selectors can potentially boost performance, most of these articles misuse the context.

Finding the Context

As of jQuery 1.3 the context is exposed as a property on the jQuery collection. Here is how you can check to see what the context is.

$('a').context; // => document

Running the above code shows that the context is the document element. In fact, the default context for all jQuery collections is the document in which jQuery was loaded. In other words, selectors are run against the whole document by default.

Changing the Context

The context needs to be a node to work properly. This is the part that is often overlooked. I’ve overlooked it in the past as well. Lots of examples out there tell you to just pass a second selector to jQuery to act as the reference node for the search. While this seems to work, it is still running the search on the whole document.

Here is an example of passing a second selector as the context. By looking at the context property you can see that the context is still the document though.

$('a', '#myContainer').context; // => document

When jQuery encounters another selector for the context it actually converts it to say the following instead.

$('#myContainer').find('a');

This conversion also happens the same way if you pass a jQuery collection as the context.

Now lets actually look at how we can change the context for the jQuery collection.

// get the node for the context
var context = $('#myContainer')[0];

// pass the context as the second argument
$('a', context).context; // => <div id="myContainer">

From this example we can see that passing a node as the second argument actually changes the context for the jQuery collection.

jQuery 1.3.3 and .live()

In jQuery 1.3.x, the .live() method binds its event handlers to the document. In the upcoming jQuery 1.3.3 release the .live() method will bind its events to the context of the jQuery collection. This means your .live() events can be more focused and potentially faster.

Posted in jQuery with 17 comments

Recent Posts

Automating with Special Events

Wednesday, June 17, 2009

David Walsh had a great idea to coerce an element that is given a click event to have a pointer for its cursor. The pointer is a universal symbol for users to know that something is clickable. Using this technique it would be “automatic” that any element g…

Posted in jQuery with 2 comments

jQuery Edge: New Special Event Hooks

Thursday, June 4, 2009

In jQuery 1.3.3 there are two new special event hooks: add and remove. These two hooks, unlike setup and teardown, are called for each event being bound. The add hook receives the handler, data, and namespaces as arguments. The remove hook receives the dat…

Posted in jQuery, jQuery Edge with 6 comments

jQuery Edge: Simplified .hover()

Thursday, May 28, 2009

In jQuery 1.3.3 the .hover() method will optionally accept a single method instead of always requiring two methods. This allows you to contain your logic within one method instead of duplicating it among two. I don’t know about you but I’ve avoided using t…

Posted in jQuery, jQuery Edge with 5 comments

jQuery Edge: Better Support for other Windows and Documents

Thursday, May 14, 2009

jQuery in the past hasn’t always played well with other windows and documents than the one it was loaded in. However, jQuery is moving in the right direction to help ease the pain of cross window/frame development. There have been several commits recently…

Posted in jQuery, jQuery Edge with 5 comments

jQuery Edge: Bind with a Different “this”

Tuesday, May 12, 2009

Brand new to jQuery SVN is the oft-requested feature of providing a different value for the “this” object in an event callback. Previously jQuery would always send the element as the value of “this” in the callback. You can utilize this new feature by pass…

Posted in jQuery, jQuery Edge with 9 comments

jQuery Edge: Live Events now with Data

Friday, May 8, 2009

In jQuery 1.3.3 the .live() method will have the ability to pass data along just like you might do with .bind(). The .bind() (and now .live()) method takes an optional second argument called “data”. The data can then be accessed within the event handler by…

Posted in jQuery, jQuery Edge with 3 comments

jQuery Edge: Versatile .index()

Thursday, May 7, 2009

Paul Irish and ajpiano (via Google Groups) saw an opportunity to make the .index() method work better for them. Currently the .index() method looks for a given element within the jQuery collection. However, they wanted a quick way to get the index of the c…

Posted in jQuery, jQuery Edge with 4 comments

jQuery Edge: Enhanced .toggleClass()

Wednesday, May 6, 2009

In jQuery 1.3.3 the .toggleClass() method will have a couple more modes of operation. Currently in jQuery 1.3.2 the .toggleClass() method can only toggle one class name at a time. The new .toggleClass() method will be able to toggle multiple class names an…

Posted in jQuery, jQuery Edge with 3 comments

Special Events

Thursday, March 26, 2009

jQuery, since 1.2.2, has had an API for “special events”. These events are special because they have the ability to do some extra work for specific events and even the option to bypass some of the internal jQuery event system. With these special events you…

Posted in jQuery with 14 comments

DRYing up Flash Embedding

Thursday, February 26, 2009

Most of the Flash embedding I’ve done has involved passing in some dynamic variables to the flash. After trying a few different techniques for passing in those variables, I believe I found a solution I’m happy with. I use the jQuery SWFObject plugin, the j…

Posted in jQuery, Ruby on Rails with 0 comments