Created
March 14, 2016 22:17
-
-
Save carlosascari/39b31573df1d452ddbf9 to your computer and use it in GitHub Desktop.
A cross browser way of parsing an html string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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