I can't find examples of vanilla javascript replaceWith using multiple elements/nodes.
Given HTML with numerous children:
<span id="parent"><span>Hardware:</span> <br>
the <span id="oldChild">physical</span> components of a <span>computer</span>.</span>
Can I use replaceWith to swap any one of the child spans, say #oldChild, with multiple elements AND text nodes (the commas and spaces following these spans):
const newSpans =
"<span id="newChild1">kickable</span>,
<span id="newChild2">throwable</span>,
<span id="newChild3">punchable</span>"
What is wrong with the syntax of the following? And how can I convert this dynamically generated code (above) into an acceptable argument for replaceWith?
oldChild.replaceWith( newSpans );
Many thanks to Phil, below:
const temp = document.createElement("div")
temp.innerHTML = newSpans
const oldChild = document.getElementById("oldChild")
oldChild.replaceWith(...temp.childNodes)
Note: Phil advises wisely it would be preferable to avoid HTML strings (ie it would be preferable to use other data structures like objects and arrays).