How to empty the contents of a div and insert a working iFrame?


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:

    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");


    if(videoContainer !== null){
        videoContainer.addEventListener('click', (ev) => {

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.


Use videoContainer.innerHTML = iframeHTML instead of videoContainer.replaceChildren(iframeHTML);

Answered By – Azamat

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.