Created
March 11, 2018 02:38
-
-
Save anonymous/66c76054704a5f66023d333a15894966 to your computer and use it in GitHub Desktop.
Content Object to HTML (source: https://jsfiddle.net/beacrea/v8q5av86/)
This file contains hidden or 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 hidden or 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