Skip to content

Instantly share code, notes, and snippets.

@folletto
Created March 8, 2012 16:10
Show Gist options
  • Save folletto/2001797 to your computer and use it in GitHub Desktop.
Save folletto/2001797 to your computer and use it in GitHub Desktop.
Dynamically parse a string to a DOM to be used with jQuery. It's very useful because it allows to parse non-XML content (either XML not declared as such or HTML)
_updateBody: function(str) {
/****************************************************************************************************
* This function creates a new document object to be used to parse a HTML string.
*
*/
var domDocument = document.implementation.createHTMLDocument();
var range = domDocument.createRange();
range.selectNode(domDocument.body);
var documentFragment = range.createContextualFragment(str);
domDocument.body.innerHTML = "";
domDocument.body.appendChild(documentFragment);
return domDocument;
}
@maartenJacobs
Copy link

Good stuff, Davide!
Gives me some motivation to dig through documentation :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment