jquery ajax parse response text


Ok this is really frusturating me because I've done this a hundred times before, and this time it isn't working. So I know I'm doing something wrong, I just can't figure it out.

I am using the jQuery .get routine to load html from another file. I don't want to use .load() because it always replaces the children of the element I'm loading content into.

Here is my .get request:

$(document).ready(function() {
    $.get('info.html', {}, function(html) {
        // debug code
        // end debug code

The file 'info.html' is a standard xhtml file with a proper doctype, and the only thing in the body is a series of ul's that I need to access. For some reason, the find function is giving me a null value.

In firebug, the GET request is showing the proper RESPONSE text and when I run


Instead of the current console.log line, I get the whole info.html as output, like I would expect.

Any ideas?


Try including whole body within a <div> tag, e.g. <body><div>content</div></body>.

By : sanmai

Wanting to do the same thing and knowing that JQuery load(..) does it, I had a look in the code. While you can't turn a complete html response directly into a JQuery object, you can append it to one so:

function(data, textStatus, xhr) {
    // Create a dummy div to hold the results,
    // inject the contents of the document into it,
    // Locate the specified elements

The response from the server that goes into data is like:

<! doctype ... >
    <div id="snippet">
      <p>Some content here that we are interested in</p>

I have found this being pretty clean solution:

var elementInResponse = $("<div>").html(responseText).find(selector);
By : Epeli

This video can help you solving your question :)
By: admin