Dynamically Create and Remove Iframe in JavaScript Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript allows us to dynamically add and remove elements to the document. We can use event listeners and special JavaScript methods to create and remove HTML elements. This article focuses on creating and removing iframe elements dynamically using JavaScript.Let's first create a basic structure of the document using buttons and other elements. Buttons will allow us to call the JavaScript methods.Flow Diagram Image for Dynamic IframeSteps to Dynamically Create and Remove Iframe in JavaScriptFirst, Create an HTML Structure for the page containing title, 2 buttons (create and remove iframe) and dummy iframe using the html elements like div's, span, iframe, buttons etc along with class name and some inline styles.Define a function createIframeElement that creates and render the iframe dynamically on the web-page and assign with create iframe onclick event.The createIframeElement uses createElement() to create iframe and its define properties like srcdoc, height and width using JavaScript.Define a function removeIframeElement that calls when removeiframe button is clicked and removes the dynamically created iframe from the page with help of removeChild() method.Example: This example demonstrates the code to dynamically create and remove iframe. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dynamically Create and Remove Iframes</title> <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity= "sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"> <style> body { background-color: #f8f9fa; font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; } h2 { color: #28a745; margin-bottom: 20px; } h3 { margin-bottom: 20px; } .iframe-container { margin-bottom: 20px; border: 2px solid #dee2e6; padding: 10px; border-radius: 8px; background-color: #ffffff; } button { margin: 5px; } .no-iframes { display: none; } </style> </head> <body> <h2>GeeksforGeeks</h2> <h3>Dynamically Create and Remove Iframes</h3> <div id="display" class="iframe-container"></div> <button class="btn btn-primary" onclick="CreateIframeElement()"> Create Iframe </button> <button class="btn btn-danger" onclick="RemoveIframeElement()"> Remove Iframe </button> <script> const CreateIframeElement = () => { // Check if there's an existing iframe to avoid multiple iframes const displayDiv = document.getElementById("display"); if (displayDiv.childElementCount > 0) { alert("An iframe already exists. Please remove the existing one before adding a new one."); return; } // Creating iframe element let el = document.createElement("iframe"); // Setting the values for the attributes el.srcdoc = ` <h1>Iframe Element</h1> <p>Hello Geek! <br> How are you?</p> `; el.width = "400px"; el.height = "200px"; el.style.border = "none"; // Optional: remove border if desired // Adding the created iframe to div as a child element displayDiv.appendChild(el); }; const RemoveIframeElement = () => { const displayDiv = document.getElementById("display"); // Check if there is at least one child to remove if (displayDiv.childElementCount === 0) { alert("No iframe to remove."); return; } // Remove the last child (iframe element) of div displayDiv.removeChild(displayDiv.lastChild); }; </script> </body> </html> Output: Comment More infoAdvertise with us Next Article Dynamically Create and Remove Iframe in JavaScript harishcarpenter Follow Improve Article Tags : HTML HTML-Questions JavaScript-Questions Similar Reads HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript 11 min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis 8 min read HTML Introduction HTML stands for Hyper Text Markup Language, which is the core language used to structure content on the web. It organizes text, images, links, and media using tags and elements that browsers can interpret. As of 2025, over 95% of websites rely on HTML alongside CSS and JavaScript, making it a fundam 6 min read HTML Tags - A to Z List HTML Tags are fundamental elements used to structure and format content on web pages. They provide instructions to web browsers on how to render text, images, links, and other media.HTML tags are enclosed in angle brackets < > and usually come in pairs: an opening tag and a closing tag. The cl 15+ min read HTML DOCTYPE Declaration HTML DOCTYPE (Document Type Declaration) is an instruction that appears at the beginning of an HTML document, before the <html> tag.Its primary role is to tell the web browser which version of HTML the page is written in, ensuring that the browser renders the content correctly. It is not an HT 4 min read 30+ Web Development Projects with Source Code [2025] Web development is one of the most in-demand career paths in the IT industry, experiencing consistent growth of around 20â25% annually. Whether you're a student starting out or an experienced professional looking to switch or advance your career, it's essential to go beyond theory and demonstrate yo 4 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read HTML Forms HTML forms, defined using the <form> Tags are essential for collecting user input on web pages. They incorporate a variety of interactive controls such as text fields, numeric inputs, email fields, password fields, checkboxes, radio buttons, and submit buttons. Over 85% of websites rely on for 5 min read Like