[jQuery] Accessing the DOM from an XHR Request
I'm sure a lot of us have run into this issue, but searching - I
couldn't find anything about it. The problem is that when you do an
Ajax request, the result is simply a string, not a document, thus you
cannot access the dom, and you cannot modify it other than by using
string-parsing methods.
One solution I have heard of it putting the whole content into a div /
iframe, then accessing it - I don't know why, but that just sounds
messy.
Here's a snippet of code I got from: http://www.thescripts.com/forum/thread478280.html
function toDOM(HTMLstring) {
var d = document.createElement('div');
d.innerHTML = HTMLstring;
var docFrag = document.createDocumentFragment();
while (d.firstChild) {
docFrag.appendChild(d.firstChild)
};
return docFrag;
}
This puts the string-HTML into a div which is created then filled with
the javascript.
Is this the best way to do this? Does jQuery have any shortcuts for
this sort of thing?