Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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:

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!