Simple jQuery ajax example not finding elements in returned HTML Simple jQuery ajax example not finding elements in returned HTML ajax ajax

Simple jQuery ajax example not finding elements in returned HTML


This is not a direct answer, but may help to clarify things.

The data parameter of the callback function can be made into a jQuery (1.6.2) object $(data), which contains the different parts of the HTML response:

  • Stuff that precedes the actual document, such as a doctype declaration, or ignorable white space textnodes.
  • The contents of the head element.
  • The contents of the body element.

The html, head and body elements are not in the data object. Since the number of elements contained in head and body may vary, you should not get them by indexing like $(data)[2]. Instead, apply a filter to this object, like this:

        $.get(          uri,          function(data, textStatus, jqXHR){            var $doc = $(data);            var title = $doc.filter('title').text();            // title is the title from the head element.            // Do whatever you need to do here.          }        );

After filtering the right elements, you can apply further selectors using find().

Unfortunately, in IE the head elements are not part of $(data). I have no idea why this is.


I've managed to load snippets off of full html-documents just fine by using .load().

To create a new block with extracted html into the DOM I do this:

$('<div></div>').appendTo('body').load('some-other-document.html div#contents');

If it's not working for you, make sure you're using the most recent version (or post 1.2) of jQuery. See the documentation for .load for more examples.

Edit:

Note, though, that with the above example the result will be:

<div><div id="contents">...</div></div>

To get just the contents of the #contents div in the other document, use a callback-function in the load-function call.

$('<div></div>').load('some-other-document.html div#contents', null,     function (responseText, textStatus, XMLHttpRequest) {        if (textStatus == success) {            $('<div></div>').appendTo('body').html($(this).html());        }    });


I found that if ajaxtest-load.html does not have <html> or <body> tags but just a few html elements, it does work.

Edit:

If the input has to be a full HTML page, maybe you can first strip of the tags you don't want with string operations.. anyone?

Edit 2:

Vaguely remembered Javascript/DOM allowed for "temporary documents" which you could operate on and use the results from, then a bit of googling yielded a parseHTML function (http://www.daniweb.com/forums/post874892-2.html) which I've adapted to return the right bit:

$(document).ready(function(){  $('input').click(function(){    $.ajax({      type : "POST",      url : 'ajaxtest-load.html',      dataType : "html",      success: function(data) {        alert( data ); // shows whole dom        var gotcha = parseHTML(data, 'wrapper');        if (gotcha) {          alert($(gotcha).html());        }else{          alert('ID not found.');        }      },      error : function() {        alert("Sorry, The requested property could not be found.");      }    });  });});function parseHTML(html, idStr) {  var root = document.createElement("div");  root.innerHTML = html;  // Get all child nodes of root div  var allChilds = root.childNodes;  for (var i = 0; i < allChilds.length; i++) {    if (allChilds[i].id && allChilds[i].id == idStr) {      return allChilds[i];    }  }  return false;}

Does that work?