I have a nodelist, nl
, and a div#parent
.
How can I set all elements from nl
as div#parent
's innerHTML
?
I have a nodelist, nl
, and a div#parent
.
How can I set all elements from nl
as div#parent
's innerHTML
?
Empty the parent first, then use appendChild to append a DOM element
parent.innerHTML = '';
parent.appendChild(nl);
for nodeLists containing several elements, iteration is needed
parent.innerHTML = '';
[].forEach.call(nl, function(item) {
parent.appendChild(item);
});
When nodelists are iterables, then you can do
for (let node of nl) parent.appendChild(node);
See Are HTMLCollection and NodeList iterables?. In the meantime, you may have some luck with
NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
Try creating a document fragment and append that instead.
var fragment = document.createDocumentFragment();
while(nl.length)
fragment.appendChild(nl[0]);
parent.appendChild(fragment);