sottenad
sottenad

Reputation: 2646

How to consolidate $.load() functions in jQuery

I have code pulling in pieces of content from another page on my domain using the hashtag to target the correct div like so: $('#content').load('http://www.mysite.com/Default.aspx #homeText');

I have to grab 5 different div's from a single page, and rather than call the $.load() function 5 times, is there a more efficient way that wont require 5 different calls to the same page. I assume there is a way to parse the incoming page, but I'm not sure what the best way would be. Any suggestions?

Upvotes: 1

Views: 45

Answers (2)

wmorrell
wmorrell

Reputation: 5317

I never tried something like this, but you should be able to do something like:

var temp = $('<div></div>').load('http://www.mysite.com/Default.aspx');
temp.find('#homeText');
temp.find('#someOtherDiv');
// etc

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69915

You can use AJAX in this case.

$.ajax({ 
   url: "http://www.mysite.com/Default.aspx",
   success: function(response){
      var $response = $(response);
      var div1 = $response.find("div1Selector");
      var div2 = $response.find("div2Selector");
      var div3 = $response.find("div3Selector");
      var div4 = $response.find("div4Selector");
      var div5 = $response.find("div5Selector");
   }
});

Upvotes: 3

Related Questions