Skip to content

Instantly share code, notes, and snippets.

@beacrea
Forked from anonymous/fiddle.html
Created March 11, 2018 02:38
Show Gist options
  • Save beacrea/0a17fd5b153d72fc851a50d541a3944c to your computer and use it in GitHub Desktop.
Save beacrea/0a17fd5b153d72fc851a50d541a3944c 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