Created
March 17, 2018 14:36
-
-
Save a1exlism/c98d312a126e9a78c304982b23976951 to your computer and use it in GitHub Desktop.
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
/** | |
* @param {String} HTML representing a single element | |
* @return {Element} | |
*/ | |
function htmlToElement(html) { | |
var template = document.createElement('template'); | |
html = html.trim(); // Never return a text node of whitespace as the result | |
template.innerHTML = html; | |
return template.content.firstChild; | |
} | |
var td = htmlToElement('<td>foo</td>'), | |
div = htmlToElement('<div><span>nested</span> <span>stuff</span></div>'); | |
/** | |
* @param {String} HTML representing any number of sibling elements | |
* @return {NodeList} | |
*/ | |
function htmlToElements(html) { | |
var template = document.createElement('template'); | |
template.innerHTML = html; | |
return template.content.childNodes; | |
} | |
var rows = htmlToElements('<tr><td>foo</td></tr><tr><td>bar</td></tr>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment