I would like to remove a <script> tag inside a <div> and add it to another <div>.
In jQuery it would be:
$('.scripts').remove().appendTo('.another-container');
It works fine in jQuery, but I would prefer to use the plain javascript method than the jQuery one. Therefore I'm trying to write in JS but it doesn't work:
const scriptTag = document.querySelector(".scripts");
const container = document.querySelector(".another-container");
for (var i = 0; i < scriptTag.length; i++) {
scriptTag[i].parentNode.removeChild(scriptTag[i]);
container.appendChild(scriptTag[i]);
}
Thank you.
I don't have enough reputation to comment but
document.querySelectordoesn't return an array. (querySelectorAlldoes)However, because nodes can only exist in one place at a time, it would be more concise to just directly append the script tag to the container