Before beginning, let's take a quick look at what jQuery does to a basic HTML page returned from an $.ajax()
call, and converting the returned data into a jQuery object.
$.ajax({
dataType : 'html',
url : 'path/to/example-page.html',
success : function(data) {
// log the result of the data converted into a jquery object.
console.log( $(data) );
}
});
Here's what you would expect to see:
[
0 <TextNode textContent="\n\n\n\n\n ">
1 title
2 <TextNode textContent="\n ">
3 meta
4 <TextNode textContent="\n\n\n\n\n">
5 div#container
6 Comment { data=" #container ", length=12, nodeName="#comment", more...}
7 <TextNode textContent="\n\n">
jquery "1.6.4"
length 8
selector ""
// additional data and functions removed for brevity
]
YIKES! That's quite ugly! Attempting to do anything with that can yield results, but you need to know what the data structure looks like every single time, and where the data lie within that object. Is that data at the root, or is it buried within?
Like previous posters have mentioned, you can use .filter()
, but the root is as far as that search will go, because you're simply filtering the returned results. However, if you were to use .find()
at this point and the element you wanted is at the root, you'll receive an empty set, but anything buried beyond the root will be found.
So, why be pinned down to needing to know what that data structure looks like with 100% certainty, and why bother going through all the trouble of having to use multiple .filter()
and .find()
calls, and dare I say an .each()
loop? Yuck! That's just simply too much work and takes way too much time.
If you want to .find()
a particular HTML element returned from an .ajax()
call, start with the following line:
var response = $('<html />').html(data);
Can it really be that easy? In fact, yes it is! What's happening here is that a new <html>
element is being created and converted into a jQuery object. This is used a starting location to insert the returned HTML from an .ajax()
call. It's kind of like doing $('html')
on a webpage. With this, you can begin finding elements.
response.find( ... ); // any jquery selector in place of the ellipsis.
Here's an example that uses the original poster's question:
$.ajax({
dataType : 'html',
url : 'path/to/example-page.html',
success : function(data) {
// set the returned contents in a new base <html> tag.
var response = $('<html />').html(data),
anchors, hrefValuesList = [ ],
i, end;
// now you can search the returned html data using .find().
anchors = response.find('a');
// grab all your href values from each anchor element.
end = anchors.length;
for (i = 0; i < end; i++) {
hrefValuesList.push( anchors[ i ].href );
}
// continue processing the data as necessary...
}
});
Obviously the above will need some refining if you want to filter out any unwanted content, or want to refine the values returned.
With that, you could see something like the following example array returned:
[ "http://stackoverflow.com/", "http://www.google.com/" ] // and so on...
Using this approach, you can easily use the power of .find()
on any HTML data returned through the $.ajax()
function like you already do on any elements you find in the DOM. The real bonus is that you aren't directly manipulating the DOM to find or do what you want, which is an expensive process.
Happy scrubbing! =)