-
-
Save beacrea/0a17fd5b153d72fc851a50d541a3944c to your computer and use it in GitHub Desktop.
Content Object to HTML (source: https://jsfiddle.net/beacrea/v8q5av86/)
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
<div id="container"></div> |
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
// 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