Say I have an inline script in my DOM that's been commented out:
<!-- <script type='text/javascript' id='my-script'>
alert('Boo!');
</script> -->
I can successfully find said comment in the DOM, but if I
const script = comment.nodeValue;
comment.replaceWith(script);
The comment does indeed get replaced in the DOM; however, it gets replaced by a text node, not a script node.
function myFunction() {
const locator = document.getElementById('foo');
let comment = locator.nextSibling;
if ( 3 === comment.nodeType ) {
comment = comment.nextSibling;
}
if ( 8 !== comment.nodeType ) {
console.warn('Oops! That\'s not a comment!');
} else {
const script = comment.nodeValue;
comment.replaceWith(script);
}
}
<div id='foo'></div>
<!-- <script type='text/javascript' id='my-script'>
alert('Boo!');
</script> -->
<button onclick="myFunction()">Click me</button>
Is there a way to insert it as a script other than by regex parsing the comment.nodeValue and manually building up a script node?