totallyNotLizards
totallyNotLizards

Reputation: 8569

how to parse returned page html with jQuery.get()

I was until recently using $.load() to get a specific piece of another page and load it into the current one, but as the user initiates those requests they can queue up and I found myself needing a way to abort them lest click-happy users break my page.

This led me to $.get() which works great and lets me abort the request if another is launched before the first returns, but now I need to parse the returned html (it's an entire page with doc-type and head elements) and only get one div from it.

How do I achieve this?

Upvotes: 8

Views: 11118

Answers (3)

Bart Edgerton
Bart Edgerton

Reputation: 1

I tried binarious example, but it returned object::object, so modified slightly:

$.get($(this).attr("href"), function(returnedHTML) {
     alert($('#content', $(returnedHTML)).html());
})

Upvotes: 0

sanmai
sanmai

Reputation: 30891

In the callback:

function(data) {
     // we wrap data with jQuery here:
     $(data).find("#id");
}

Upvotes: 8

binarious
binarious

Reputation: 4588

This should work:

$.get('ajax/test.html', function(data) {
    var my_div = $('#my_div', $(data));
});

Upvotes: 10

Related Questions