Skip to content

Instantly share code, notes, and snippets.

@carlosascari
Created March 14, 2016 22:17
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 carlosascari/39b31573df1d452ddbf9 to your computer and use it in GitHub Desktop.
Save carlosascari/39b31573df1d452ddbf9 to your computer and use it in GitHub Desktop.
A cross browser way of parsing an html string.
/**
* A cross browser way of parsing a html string similar to jQuery's `$(<HTML String>)`
*
* @method parseHTML
* @param htmlString {String}
* @return {HTMLElement}
*/
function parseHTML(htmlString)
{
var html = document.createDocumentFragment()
var container = document.createElement('div')
container.innerHTML = htmlString
var nextElement = container.firstChild
while (nextElement)
{
html.appendChild(nextElement)
nextElement = container.firstChild
}
return html
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment