ndesign11
ndesign11

Reputation: 1779

How to use .append on a jQuery .load() function

I'm trying to append the data being called rather than replacing it but I'm not sure why .append isn't working.

      $('.incoming3').load('t3.html #test3').append;

Upvotes: 0

Views: 172

Answers (3)

charlietfl
charlietfl

Reputation: 171700

load() method by design replaces the html of the selector. You will need to create your own filtering AJX method.

$.get( 't3.html', function(response){
   var contentDesired=$(response).find( '#test3')/* or filter instead of find if no body tag wrapping html*/
   $('.incoming3').append( contentDesired)

})

This is virtually identical to one solution I gave you on same situation in your last question for same issue

Upvotes: 1

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

$('.incoming3').append($('<div>').load('t3.html #test3'));

or

$.get('t3.html', function(data) {
    $('.incoming3').append($(data).find('#test3'));
});

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150070

Well first of all .append() is a function - you need to use parentheses after it, and supply it with a parameter telling it what to append. Second, you can't just change the way .load() works by chaining .append() onto the end of it.

Try this instead:

$.get("t3.html", function(data) {
    $('.incoming3').append($(data).filter("#test3"));
});

That is, use the $.get() method to retrieve the data, wrap the data in a jQuery object and use the .filter() method to extract the bit you need, and append that bit to your element.

Upvotes: 2

Related Questions