Using jQuery.ajax rather than jQuery.load

jQuery.load is an incredibly useful method, but if you want to do more with the returned source than put it into a div then you may want to use jQuery.ajax directly with your own callback.

The source code for the load method is a great starting point for writing your own callback, in particular how the ‘done’ callback selects the items to append;

jQuery("<div>").append( jQuery.parseHTML( responseText ) ).find( selector );

This is most likely what you’re looking to amend by using your own callback. The parseHTML method creates an HTML fragment and then appends it to a div (this div is an orphan, it is never inserted into the DOM).

You may, for example want to run two selects;

tempDiv = jQuery("<div>").append(jQuery.parseHTML( responseText ));

someResult = tempDiv.find( selector1 );
someOtherResult = tempDiv.find( selector2 );
Filed under: JavaScript