In Javascript, I am trying to dynamically create an HTML element, append an
element as its child, clone the template\'s
Note, var div = document.createElement('div').appendChild(h1)
sets div
variable to h1
, the appended element, not div
element; see What is the behavior of document.createElement when passed as an argument?.
Set .innerHTML
of to
.outerHTML
of div
element, call .appendChild()
chained to document.body
with temp.content
as parameter.
window.onload = function() {
var temp = document.createElement('template');
var h1 = document.createElement('h1');
h1.textContent = 'hello';
var div = document.createElement('div');
div.appendChild(h1);
temp.innerHTML = div.outerHTML;
console.log('temp: ', temp.content);
document.body.appendChild(temp.content);
}