1var h = document.getElementById("myH2");
2h.insertAdjacentHTML("afterend", "<p>My new paragraph</p>");
1 //adding 'p' tag to body
2
3 var tag = document.createElement("p"); // <p></p>
4 var text = document.createTextNode("TEST TEXT");
5 tag.appendChild(text); // <p>TEST TEXT</p>
6 var element = document.getElementsByTagName("body")[0];
7 element.appendChild(tag); // <body> <p>TEST TEXT</p> </body>
1function create(htmlStr) {
2 var frag = document.createDocumentFragment(),
3 temp = document.createElement('div');
4 temp.innerHTML = htmlStr;
5 while (temp.firstChild) {
6 frag.appendChild(temp.firstChild);
7 }
8 return frag;
9}
10
11var fragment = create('<div>Hello!</div><p>...</p>');
12// You can use native DOM methods to insert the fragment:
13document.body.insertBefore(fragment, document.body.childNodes[0]);
14
1//create div
2let div = document.createElement('div');
3//create button
4let button = document.createElement('button');