Skip to content

Instantly share code, notes, and snippets.

@ChaseWest
Created May 10, 2014 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaseWest/1027e5ef3166f7d5126f to your computer and use it in GitHub Desktop.
Save ChaseWest/1027e5ef3166f7d5126f to your computer and use it in GitHub Desktop.
Parse HTML string without DOMParser API
var HTML = "<div><span>Test</span></div>";
var body = document.querySelector("body");
function parseHTML(htmlString){
var frag = document.createDocumentFragment(),
el = document.createElement( "div" );
el.setAttribute("id", "wrapper");
el.innerHTML = HTML;
frag.appendChild(el);
return frag.querySelector("div#wrapper").children[0];
}
console.log(parseHTML(HTML));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment