I am trying to create a function that empties a video container of its contents (thumbnail / playbutton) and replace it with an iframe of for example vimeo or youtube.
So far I got this:
replaceIframe(){
const parser = new DOMParser();
const videoContainer = document.querySelector('#landingspage .video-container');
const iframe = document.querySelector('#landingspage .iframe-container').innerHTML;
const iframeHTML = parser.parseFromString(iframe, "text/html");
console.log(iframeHTML);
if(videoContainer !== null){
videoContainer.addEventListener('click', (ev) => {
ev.preventDefault();
videoContainer.replaceChildren(iframeHTML);
});
}
}
But this gives me the error:
Uncaught DOMException: Failed to execute 'replaceChildren' on 'Element': Nodes of type '#document' may not be inserted inside nodes of type 'DIV'.
at HTMLDivElement.
How can I fix this? I first tried it without the parser object but then it just replaces the contents with a string of the iframe html.
>Solution :
Use videoContainer.innerHTML = iframeHTML instead of videoContainer.replaceChildren(iframeHTML);