When some html string added to DocumentFragment innerHTML
property, there are no children exists. But same action with Element created by createElement
show children. I created simple example for comparing innerHTML behavior for DocumentFragment and simple Element:
const fragment = document.createDocumentFragment();
const div = document.createElement('div')
fragment.innerHTML = "<i>Test</i>";
console.log('fragment children:', fragment.children); // empty
fragment.innerHTML = "<i><b>Test</b></i>";
console.log('fragment children:', fragment.children); // empty
div.innerHTML = "<i>Test</i>";
console.log('div children:', div.children) // has children
div.innerHTML = "<i><b>Test</b></i>";
console.log('div children:', div.children) // has children
But fragment can show children added by appendChild:
const span = document.createElement('span');
const fragment2 = document.createDocumentFragment();
fragment2.appendChild(span);
console.log('fragment children for appendChild', fragment2.children);
How to fix this weird DocumentFragment behavior ?
ADDITIONAL: I need DocumentFragment as temporary HTML container.
JSFiddle with reproducing the problem.