I have a function returning a template literal:
function generateStuff(nodes) {
const output = `<ul>${nodes}</ul>`;
return document.body.innerHTML = output;
}
nodes is an array of <li> elements made with createElement and then added to nodes via appendChild.
Is there a way of render a list with generateStuff(nodes)? Right now all it returns is <ul>[object NodeList]</ul> and I just want it to return proper HTML with working links etc.
I suppose I need to parse it somehow but as this is JS and NodeList is native to it maybe there's a method in place for it already?
Yes, but I'd consider changing how you're doing this instead. But the way you'd do it is with a round-trip through HTML:
But that's inefficient and lossy (loses event handlers on the nodes, for instance). Instead of using a template literal, consider appending the nodes directly:
In a comment you've said:
You could create the structure by assigning to
innerHTMLwith a means of identifying theul, then once the structure exists, do the append:Live Example: