Mike
Mike

Reputation: 3418

How to get a part of HTML webpage in a jQuery ajax Request? In other words how to filter the web page contents on an ajax call?

I am using the below script to load the html in my page. However it loads all of the page contents that is included in webpage.html means(from doctype to closing html tag).

$(function() {
            $.ajax({
                type:"GET",
                url:"webpage.html",
                dataType:"html",
                success:function(data) {
                    alert(data);
                    $("body").html(data);
                },
                error:function() {
                    alert("Error");
                }
            })
        });

I have no control over webpage.html, I actually just need a few divs with particular classes to load after an ajax Request. Can anyone give me some Idea as to how would I filter the webpage contents over an ajax Call for which I have no control.

Thanks, Mike

Upvotes: 1

Views: 12620

Answers (2)

Jonny Sooter
Jonny Sooter

Reputation: 2417

Use .load()

var url = "webpage.html .classOfDivsYouWantLoaded";

$("body").load(url, function(){
    //callback after your data is in loaded into body.
});

Upvotes: 2

maximkou
maximkou

Reputation: 5332

 success:function(data) {
     var out = "";
     $(data).find("your selectors").each(function(loop, item){
         out += $(item).html();
     });
     data = out;
     alert(data);
     $("body").html(data);
 }

Upvotes: 3

Related Questions