Skip to content

Instantly share code, notes, and snippets.

Created March 11, 2018 02:38
Show Gist options
  • Save anonymous/66c76054704a5f66023d333a15894966 to your computer and use it in GitHub Desktop.
Save anonymous/66c76054704a5f66023d333a15894966 to your computer and use it in GitHub Desktop.
Content Object to HTML (source: https://jsfiddle.net/beacrea/v8q5av86/)
<div id="container"></div>
// Parsing function
let parseHTML = function(str) {
let tmp = document.implementation.createHTMLDocument();
tmp.body.innerHTML = str;
return tmp.body.children;
};
// Initial content
let string = ["<h1>Hello</h1>"];
let object = {
"p1": "Yo, this is a story",
"p2": "all about how",
"p3": "my life got flipped turned upside down"
};
// Add HTML markup
for (var prop in object) {
content = "<p>" + object[prop] + "</p>";
string.push(content);
}
// Join all items to a single string
string = string.join('');
// Render
document.getElementById("container").innerHTML = string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment