Mike Vierwind
Mike Vierwind

Reputation: 1520

Loop with load jquery

I make this script.

var arr = $('.siblings-list li').map(function() { return $(this).text() });
for (var i = 0; i < arr.length; i++) {
    $('.main').load(arr[i] + ' .section', function() {

    });
}

With this script. I get a array with url's and load the section of the url's in the main div. But i have a question with the load methode of jQuery. Now the script loads the section of the url's in the main div. And remove the html that's in the div. How can i change the script. That the current html in the main div not removed?

Thank you

Upvotes: 1

Views: 853

Answers (2)

owenmelbz
owenmelbz

Reputation: 6584

When you use .load it overwrites the complete content of the selector, in this case .main is getting overwritten every time.

Try

$(".main").append($("<div>").load(arr[i] + ' .section', function() {

see how that works?

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

$.get(arr[i]).done(function (html) {
    $(".main").append($(html).find(".section"));
});

However this is somewhat inefficient. Ideally you would be able to return all of the HTML you need (and only the HTML you need) with a single request.

Upvotes: 2

Related Questions