PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Optimizing jQuery with Context

Posted by teejay on November 11, 2009

The context is the optional argument in jQuery selectors.

$('a', '#context)

The context is the scope wherein the selector is matched against. A 'normal' selector would be matched against the whole document. Limiting the scope by using the context argument could potentially improve selector performance.

While you can use the optional argument in the function there are still other ways to limit the context of selectors:

// By searching descendants by using find
$('#your_div').find('a');
// By matching a descendant element
$('#your_div a');

Optimization can work wonders depending on the size of you DOM. So, what are you waiting for? Optimize away!

Categories: Web Development

Tags: javascript, jquery

No Comments

Comments are not allowed