//adding 'p' tag to body
var tag = document.createElement("p"); // <p></p>
var text = document.createTextNode("TEST TEXT");
tag.appendChild(text); // <p>TEST TEXT</p>
var element = document.getElementsByTagName("body")[0];
element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>
var p = document.createElement("p");
var text = document.createTextNode("This is a new element");
p.appendChild(text);
var element = document.getElementById("new");
element.appendChild(p);
function create(htmlStr) {
var frag = document.createDocumentFragment(),
temp = document.createElement('div');
temp.innerHTML = htmlStr;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
return frag;
}
e = `<p>your html here </p>`
var fragment = create(e);
// You can use native DOM methods to insert the fragment:
document.body.insertBefore(fragment, document.body.childNodes[0]);