O'Reilly logo

jQuery 1.3 with PHP by Kae Verens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

JavaScript optimizations

The Javascript optimization tricks are not specific to jQuery, but are very good to know.

Chaining

Chaining means running a number of functions on a selected element or group of elements.

Consider what happens when you do this:

$('.someclass').click(function(){
alert('clicked');
});
$('.someclass').css('background','red');
$('.someclass').text('some text');

On each line, jQuery is called with a fresh request to get all elements with the class name someclass and manipulate them in some way. This is quite expensive to do, as jQuery does not automatically cache the requests, so it's a fresh search every time.

To speed this up, you should chain the functions like this:

$('.someclass').click(function(){ alert('clicked'); }) .css('background','red') ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required