I am using below code to get a page:
$.get('http://example.com/page1.html', function (data) {
});
Now lets imagine there is a #content div inside that page1.html and I need to read it's inner html as I already have #content div on page where Ajax call is occurring.
What is the right way to do this? I've tried with:
data = $(data).find('#content').html();
$("#content").empty().append(data);
But it seems that html() function is not the right one as it returns null, while contents() is returning data but I am not skillful enough to get only what I need from it.
Any help appreciated, thanks!!
this code
data = $(data).find('#content').html();
return null when data is
var data ="<div id='content'>my data</div>";
because #content
already parent, if data is
var data ="<div><div id='content'>my data</div><div>";
then return exact html because this time #content is child and you can .find() this in parent div.
Untested:
var content = $("#content", data).html(); // or var content = $("#content", $(data)).html();
$("#content").html(content);
EDIT: How about this?
$('#content').load("http://example.com/page1.html #content");
From: http://api.jquery.com/load/ - "loading page fragments".