Reputation:
I have a nodelist, nl
, and a div#parent
.
How can I set all elements from nl
as div#parent
's innerHTML
?
Upvotes: 2
Views: 4448
Reputation:
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];
Upvotes: 2
Reputation: 318222
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);
});
Upvotes: 2
Reputation: 25954
Try creating a document fragment and append that instead.
var fragment = document.createDocumentFragment();
while(nl.length)
fragment.appendChild(nl[0]);
parent.appendChild(fragment);
Upvotes: 1